使用 AFNetworking 上传文件
我的后端编码器为我提供了文件上传的curl示例:
curl -v -F [email protected ]-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 字段为空的信息。可能是我不明白curl? @file.name 只是替换字段中的编码数据,对吧?
My backend coder provided me with curl example of file uploading:
curl -v -F [email protected] -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"];
}];
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须修改appendPartWithFileData:mimeType:name:方法。
在这一行上,
我写的是表单数据,而不是文件。
I had to modify appendPartWithFileData:mimeType:name: method.
On this line
instead of file i wrote form-data.