开发者

Upload file with AFNetworking

开发者 https://www.devze.com 2023-04-12 04:59 出处:网络
My backend coder provided me with curl example of file uploading: curl -v -F dictation_file=@file.name -F category=1 http://my.server.com/files/create/

My backend coder provided me with curl example of file uploading:

curl -v -F dictation_file=@file.name -F category=1 http://my.server.com/files/create/

I'm using AFNetworking and the following code is not working:

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:title forKey:@"title"];
    [parameters setObject:category forKey:@"category"];

    NSMutableURLRequest *request = [self multipartFormRequestWithMethod:@"POST" 
                                                                   path:@"files/create/" 
                                                             parameters:parameters 
                                              constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
                                                  NSData *data= [NSData dataWithContentsOfFile:record.filePath];
                                                  [formData appendPartWithFileData:data mimeType:@"application/octet-stream" name:@"dictation_file"];
     开发者_如何学JAVA                                         }];

    AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request 
                                                                          completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) 
    { // handlecodehere
}];

However in response json i receive information that dictation_file field is empty. May be i don't understand curl? @file.name simply substitude encoded data in field, right?


I had to modify appendPartWithFileData:mimeType:name: method.

On this line

[mutableHeaders setValue:[NSString stringWithFormat:@"file; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"]; 

instead of file i wrote form-data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号