我的后端编码器为我提供了文件上传的 curl 示例:
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 只是在字段中替换编码数据,对吗?