【问题标题】:Upload image to server from imagePickerController AFNetWorking 3.0从 imagePickerController AFNetWorking 3.0 上传图像到服务器
【发布时间】:2017-11-08 11:09:25
【问题描述】:
如何使用AFNetworking 将图像从imagePickerController 上传到服务器?我试过这个:
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {
[formData appendPartWithFileData:photo.data name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
}];
但我必须从哪里获取名称和文件名?
如果有人可以帮助我。
谢谢。
【问题讨论】:
标签:
ios
objective-c
afnetworking
uiimagepickercontroller
afnetworking-3
【解决方案1】:
检查AFURLRequestSerialization.h 文件以阅读您正在使用的方法的文档:
/**
Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary.
@param data The data to be encoded and appended to the form data.
@param name The name to be associated with the specified data. This parameter must not be `nil`.
@param fileName The filename to be associated with the specified data. This parameter must not be `nil`.
@param mimeType The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be `nil`.
*/
只需将文件名设置为您想在后端接收的任何内容。如果它不需要特殊的文件名,只需给它类似@"photo.jpg"