AFNetworking HTTP POST upload image stopping when

2019-07-22 20:31发布

I am using the code below to HTTP POST a multi-part web form, including a JPEG image.

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];

Web form:

<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="" />

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.

Edit:

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.

Edit 2:

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.

1条回答
仙女界的扛把子
2楼-- · 2019-07-22 21:06

Have you tried to print the NSError in the failure callback? Maybe the server is expecting another mime type.

查看更多
登录 后发表回答