繁体   English   中英

iOS AFNetworking:在通过多部分/表单数据请求上传图像时需要帮助

[英]iOS AFNetworking: Need help in uploading an image through multipart/form-data request

我正在尝试将图像从我的iOS本机应用程序上传到Restful Web服务,这是一个多部分/表单数据请求。 我为此使用AFNetworking库。

该部分的HTML版本如下所示:

<form action="rest/face/face8" method="post"
enctype="multipart/form-data">

UserId: <input type="text" name="userId"
value="cced82c0-513f-4125-95ac-ac37b3b58ee6"><br>

Enrollment Id: <input type="text" name="enrollmentId"
value="c7d8466a-fe55-454e-aad7-964f13696899"><br>

LicenseId: <input type="text" name="licenseId"
value="98a847ed-4148-4024-8265-6d3748468c3d"><br>

ClientVersion: <input type="text" name="clientVersion"
value="sdk 3.3.4 - iPhone 5 (GSM) - iOS 7.0.2"><br>

Select a file : <input type="file" name="samples" size="45" /><br>

<input type="submit" value="Upload It" />

我正在尝试在iOS应用中复制它。 我的Objective-C代码如下所示:

UIImage *image= [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMG_0095" ofType:@"jpg"]];
NSData *photoData= UIImageJPEGRepresentation(image, 0.8);

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.43.41:7001"]];
[client setDefaultHeader:@"multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY" value:@"Content-Type"];
[client setDefaultHeader:@"close" value:@"Proxy-Connection"];
[client setDefaultHeader:@"close" value:@"Connection"];
//[client setDefaultHeader:[NSString stringWithFormat:@"%d", [photoData length]]  value:@"Content-Length"];
[client setDefaultHeader:@"Mobile 2.1.0 (iPhone; iPhone OS 6.1.3; en_US)" value:@"User-Agent"];


NSMutableURLRequest *request1 = [client multipartFormRequestWithMethod:@"POST" path:@"/Biometric/rest/face/face8" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"cced82c0-513f-4125-95ac-ac37b3b58ee6"] dataUsingEncoding:NSUTF8StringEncoding] name:@"userId"];
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"c7d8466a-fe55-454e-aad7-964f13696899"] dataUsingEncoding:NSUTF8StringEncoding] name:@"enrollmentId"];
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"98a847ed-4148-4024-8265-6d3748468c3d"] dataUsingEncoding:NSUTF8StringEncoding] name:@"licenseId"];
    [formData appendPartWithFormData:[[NSString stringWithFormat:@"sdk 6.0.0 - iPhone 4s (GSM) - iOS 6.1.3"] dataUsingEncoding:NSUTF8StringEncoding] name:@"clientVersion"];
    [formData appendPartWithFileData:photoData name:@"samples" fileName:@"file" mimeType:@"application/octet-stream"];
}];

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminate;
hud.labelText = @"Connecting to Middleware...";

[request1 setTimeoutInterval:180];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request1];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
    float progress = totalBytesWritten / (float)totalBytesExpectedToWrite;
    hud.progress= progress;
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     [MBProgressHUD hideHUDForView:self.view animated:YES];
     NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
     NSLog(@"response headers: %@", [[operation response] allHeaderFields]);
     NSLog(@"response: %@",jsons);

 }
                                 failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     [MBProgressHUD hideHUDForView:self.view animated:YES];
     if([operation.response statusCode] == 403)
     {
         NSLog(@"Upload Failed");
         return;
     }
     NSLog(@"error: %@", [error debugDescription]);

 }];

[operation start];

一段时间后,我收到**请求超时错误**。 它甚至没有到达服务器端平台。 请告诉我我该怎么办。 谢谢 ...

在您的BaseURL的末尾添加/ ,因此它应如下所示

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.43.41:7001/"]];

然后在下面的代码中从您的网址中删除/ ,然后查看是否可行

NSMutableURLRequest *request1 = [client multipartFormRequestWithMethod:@"POST" path:@"Biometric/rest/face/face8" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM