AFNetworking HTTP POST上传图像在近完成时停止 [英] AFNetworking HTTP POST upload image stopping when near completion
问题描述
我使用下面的代码HTTP POST一个多部分的网络表单,包括一个JPEG图像。
I am using the code below to HTTP POST a multi-part web form, including a JPEG image.
代码: p>
Code:
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:nameField.text forKey:@"name"];
[params setObject:emailField.text forKey:@"email"];
[params setObject:titleField.text forKey:@"title"];
[params setObject:dateString forKey:@"link"];
[params setObject:descriptionTV.text forKey:@"content"];
[params setObject:tag forKey:@"tags"];
NSData* sendData = UIImageJPEGRepresentation(uploadedPhoto.image, 1.0);
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com/"]];
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST"
path:@"form"
parameters:params
constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
{
[formData appendPartWithFileData:sendData
name:@"image"
fileName:@"image.jpeg"
mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"SUCCESS! %@", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"FAILED!!! %@", operation.responseString);
}];
[operation start];
网络表单:
<input type="text" name="name" id="name" value="" />
<input type="text" name="email" id="email" value="" />
<input type="text" name="title" id="title" value="" />
<input type="text" name="link" id="link" value="" />
<input type="text" name="content" id="content" value="" />
<input type="text" name="tags" id="tags" value="" />
<input type="file" name="image" id="image" value="" />
每次运行代码时,图像上传都会顺利进行,直到上传进度完成(〜99.5%完成)。操作的 completionBlockWithSuccess:failure:
阻止从不启动,应用程序刚刚冻结。
Every time I run the code, the image upload goes smooth until when the upload progress is about to be done (~99.5% completed). The operation's completionBlockWithSuccess:failure:
blocks are never launched, and the app just freezes there.
任何人都可以帮助我跟这个?我不知道我在这里做错了。
Can anyone help me out with this? I have no idea what I'm doing wrong here.
编辑:
我做了一些更多的调查,发现它做同样,即使一个无效的请求路径,但我很确定我没有错误地键入我的表单路径。此外,当我用 nil
替换 constructedBodyWithBlock
块时,它将正常工作。
I did a bit more investigation and found that it does the same even with an invalid request path, but I'm pretty sure I didn't type my form path wrongly. Also, when I replace the constructingBodyWithBlock
block with nil
, it will work properly.
编辑2:
我在AFNetworking的Github上做了一些窥探,发现一个Github问题帖子和一个类似的帖子在Stack Overflow 。我尝试使用 [httpClient enqueueHTTPRequestOperation:operation];
而不是 [操作开始]; $ c> httpClient
一个单例,但同样的问题仍然存在。
I did some snooping on AFNetworking's Github and found a Github issue post and a similar post on Stack Overflow. I tried the using [httpClient enqueueHTTPRequestOperation:operation];
instead of [operation start];
and making the httpClient
a singleton, but the same problem still persists.
推荐答案
在故障回调中打印 NSError
?可能服务器正在等待另一个MIME类型。
Have you tried to print the NSError
in the failure callback? Maybe the server is expecting another mime type.
这篇关于AFNetworking HTTP POST上传图像在近完成时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!