从imagePickerController AFNetWorking 3.0将图像上传到服务器

时间:2017-11-08 11:09:25

标签: ios objective-c afnetworking uiimagepickercontroller afnetworking-3

如何使用imagePickerControllerAFNetworking将图像上传到服务器?我试过这个:

    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"];
}];

但是从哪里获取name和fileName?

如果有人可以帮助我。

感谢。

1 个答案:

答案 0 :(得分: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"