将图像上传到ios中的Web服务
Posted
技术标签:
【中文标题】将图像上传到ios中的Web服务【英文标题】:Upload image to web service in ios 【发布时间】:2013-12-10 07:08:27 【问题描述】:我尝试AFNetworking 的“创建上传任务”和this 将图像上传到.net 服务器,但不能。这是我尝试过的。
- (void)uploadJPEGImage:(NSString*)requestURL image:(UIImage*)image
NSURL *url = [[NSURL alloc] initWithString:requestURL];
NSMutableURLRequest *urequest = [NSMutableURLRequest requestWithURL:url];
[urequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[urequest setHTTPShouldHandleCookies:NO];
[urequest setTimeoutInterval:60];
[urequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[urequest setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// add image data
NSData *imageData = UIImageJPEGRepresentation(qrCodeImage.image, 1.0);
if (imageData)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", pictureName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urequest setHTTPBody:body];
NSLog(@"Check response if image was uploaded after this log");
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:urequest returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
由于AFNetworking代码给出,我this尽管添加了运行时错误
self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml", @"text/html", nil];
谢谢。
【问题讨论】:
代码中有一行注释掉了,它在数据中添加了Content-Disposition
行。您在执行多部分帖子时需要它,因此请取消注释。
我把它注释掉了,因为我已经在应用程序本身中生成了二维码,因此图像不会有任何路径。
您需要在多部分有效负载中包含该行。这很关键。该行中的 name
值是参数名称,而不是文件路径,例如image
或 photo
.
我添加了imageView.image而不是pictureName,但是得到了网页的html响应。我如何检查图像是否上传到提到的 url,因为我期待 "imageurl":"trivetts.iconnectgroup.com/uploads/TruckTrackUpload/…" 响应
Content-Disposition
行需要读作[body appendData:[@"Content-Disposition: form-data; name=\"FileUpload1\" filename=\"image.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
【参考方案1】:
您的代码缺少 __VIEWSTATE 内容标准。我在代码中添加了最后提取上传图片的 URL:
- (void)uploadJPEGImage:(NSString*)requestURL image:(UIImage*)image
NSURL *url = [[NSURL alloc] initWithString:requestURL];
NSMutableURLRequest *urequest = [NSMutableURLRequest requestWithURL:url];
[urequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[urequest setHTTPShouldHandleCookies:NO];
[urequest setTimeoutInterval:60];
[urequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[urequest setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// Add __VIEWSTATE
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"__VIEWSTATE\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"/wEPDwUKLTQwMjY2MDA0M2RkXtxyHItfb0ALigfUBOEHb/mYssynfUoTDJNZt/K8pDs=" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if (imageData)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"FileUpload1\"; filename=\"image.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urequest setHTTPBody:body];
NSLog(@"Check response if image was uploaded after this log");
//return and test
NSHTTPURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:urequest returningResponse:&response error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
// Extract the imageurl
NSArray *parts = [returnString componentsSeparatedByString:@"\r\n"];
if (parts.count > 0)
NSError *error = NULL;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[parts[0] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", result[@"imageurl"]); // Will either be nil or a URL string
【讨论】:
【参考方案2】:1.我想你错过了上传页面中的隐藏输入:
2.使用下面的代码,我可以成功上传并得到正确的响应字符串:
UIImage *image = [UIImage imageNamed:@"a.jpg"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer new];
// add hidden parameter here
NSDictionary * parameters = @@"__VIEWSTATE":@"/wEPDwUKLTQwMjY2MDA0M2RkXtxyHItfb0ALigfUBOEHb/mYssynfUoTDJNZt/K8pDs=";
[manager POST:@"http://cnapi.iconnectgroup.com/upload/fileuploadnew.aspx" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
// add image date here
[formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5) name:@"FileUpload1" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSData * data = (NSData *)responseObject;
NSLog(@"Success,Response string: %@", [NSString stringWithCString:[data bytes] encoding:NSISOLatin1StringEncoding]);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
【讨论】:
以上是关于将图像上传到ios中的Web服务的主要内容,如果未能解决你的问题,请参考以下文章
将图像从 ios swift 3 上传到 jsp Web 服务
我们如何将图像或照片上传到 ejabberd XMPP Web 服务器?
图像未通过 IOS 中的 POST 方法发送到 PHP Web 服务
如何使用来自iPhone应用程序的SOAP请求将图像+ xml上传到Web服务器