iPhone:NSData和字节对话问题?
我想将iPhone图像发送到服务器数据库。我编写了以下代码。我遇到数据不匹配异常,无法解决它。谁能帮我一下吗 iPhone端的代码:iPhone:NSData和字节对话问题?,iphone,image-processing,byte,asihttprequest,nsdata,Iphone,Image Processing,Byte,Asihttprequest,Nsdata,我想将iPhone图像发送到服务器数据库。我编写了以下代码。我遇到数据不匹配异常,无法解决它。谁能帮我一下吗 iPhone端的代码: - (void) uploadUserInfo:(NSString*)userId profilePic:(UIImage*)profilePic { __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString:[NSStri
- (void) uploadUserInfo:(NSString*)userId profilePic:(UIImage*)profilePic {
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/%d/uploadUserInfo?cultureDetect=false", dataUrl, currentAppID]]];
[request setPostValue:userId forKey: @"userId"];
// Upload an image
[request addPostValue:@"pic.jpg" forKey:@"fileName"];
NSData* imageData = UIImageJPEGRepresentation(profilePic, 1);
[request setData:imageData withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"profilePic"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[queue addOperation: request];
[queue go];
}
ASP.NET MVC网站控制器上的代码:
public void uploadUserInfo(string userId, string fileName, byte[] profilePic)
{
int ID;
if (int.TryParse(appId, out ID) && fileName != null && profilePic != null)
{
Image model = new Image();
System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(profilePic));
System.Drawing.Point maxSize = FileUtils.GetImageMaxSize(Image.ImageTypeEnum.TableCellImage);
System.Drawing.Image thumb = ImageUtilities.GetFixedSizeImage(image, InterpolationMode.HighQualityBicubic, null, PixelFormat.Format32bppArgb, maxSize.X, maxSize.Y, false);
byte[] resizedContent = ImageUtilities.GetBytesFromImage(thumb, ImageFormat.Png);
model.AmendedDateTime = DateTime.Now;
model.ApplicationId = ID;
model.ImageType = Image.ImageTypeEnum.TableCellImage;
string contentType = ImageUtilities.GetContentType(resizedContent);
FileInfo f = new FileInfo(fileName);
string name = f.Name.Substring(0, f.Name.Length - f.Extension.Length);
string extension = f.Extension.Substring(1);
BinaryFile file = new BinaryFile()
{
ContentType = contentType,
FileName = name,
FileExtension = extension
};
file.Data = resizedContent;
model.BinaryFile = file;
this.DataContext.Images.Table.InsertOnSubmit(model);
ValidateAndSubmitChanges();
}
return;
}
在我的方法开始执行之前,我遇到了以下错误。i、 我猜MVC引擎在数据类型不匹配时直接抛出异常
[FormatException]: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
at System.Convert.FromBase64String(String s)
at System.Web.Mvc.ByteArrayModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__4()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
转换为Base64会出现相同的异常
使用ASIHTTPRequest内置静态方法:
NSData* imageData = UIImageJPEGRepresentation(profilePic, 1);
[request setData:[ASIHTTPRequest base64forData:imageData] withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"profilePic"];
使用线程中给出的方法:
从您列出的错误中:
[FormatException]: The input is not a valid Base-64 string as it contains a non-base 64 character...
我猜您需要对上传到服务器的数据进行Base64编码
因此,我建议使用Base64库对数据进行编码
并找出如何做到这一点。我在自己的代码中使用的NSData类别在Alex Reynolds编写的答案中。这让我非常恼火!我只需要将Byte[]替换为HttpPostedFileBase,它会自动处理发布的文件 iPhone代码:
- (void) uploadUserInfo:(NSString*)userId profilePic:(UIImage*)profilePic {
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/app/%d/phone/uploadUserInfo?cultureDetect=false", dataUrl, currentAppID]]];
NSData* imageData = UIImageJPEGRepresentation(profilePic, 1);
[request setData:imageData withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"imageFile"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[queue addOperation: request];
[queue go];
}
ASP.NET MVC控制器代码:
public void uploadUserInfo(HttpPostedFileBase imageFile)
{
// Process imageFile
}
嗨,迈克尔,谢谢你的信息。我已经通过了你发给我的链接。我尝试使用[ASIHTTPRequest base64forData:imageData]静态方法,正如在我的原始问题中编辑的那样,但它给出了相同的异常!此外,如果我删除发送图像数据,那么其他一切工作都很好!所以objective-c上的NSData和ASP.NET MVC上的byte一定有关系!不知道发生了什么。我只是尝试使用亚历克斯·雷诺兹(Alex Reynolds)创作的那些类别,但同样的例外!编辑了这个问题。
- (void) uploadUserInfo:(NSString*)userId profilePic:(UIImage*)profilePic {
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"%@/app/%d/phone/uploadUserInfo?cultureDetect=false", dataUrl, currentAppID]]];
NSData* imageData = UIImageJPEGRepresentation(profilePic, 1);
[request setData:imageData withFileName:@"pic.jpg" andContentType:@"image/jpeg" forKey:@"imageFile"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[queue addOperation: request];
[queue go];
}
public void uploadUserInfo(HttpPostedFileBase imageFile)
{
// Process imageFile
}