我的后端编码器为我提供了文件上传的卷曲示例:
curl -v -F dictation_file=@file.name -F category = 1 http://my.server.com/files/create/
我正在使用AFNetworking,以下代码无效:
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"];
}];
AFHTTPRequestOperation *operation = [AFHTTPRequestOperation operationWithRequest:request
completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error)
{ // handlecodehere
}];
然而,作为回应,json我收到dictation_file字段为空的信息。可能是我不明白卷曲? @ file.name只是字段中的代理编码数据,对吗?
答案 0 :(得分:0)
我必须修改appendPartWithFileData:mimeType:name:method。
在这一行
[mutableHeaders setValue:[NSString stringWithFormat:@"file; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
而不是文件我写了form-data。