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

标签 ios objective-c afnetworking uiimagepickercontroller afnetworking-3

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

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

但是我必须从哪里获取名称和文件名?

如果有人可以帮助我。

谢谢。

最佳答案

检查 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"

关于ios - 从 imagePickerController AFNetWorking 3.0 将图像上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47178172/

相关文章:

ios - 如何通过代码关闭 uitableview 中打开的单元格 (Objective-C)

ios - 当模态视图 Controller 已经显示时如何更改 modalPresentationStyle?

ios - 将数据发送到 UITableViewCell - 数据不会发送

ios - UITableViewCell 图像在选择之前不会显示

ios - 报告 UITableview reloadData 崩溃

iphone - 在 Cocos2d 中是否可以为单个对象使用多个标签?

ios - 使用自定义过滤器以图像形状显示自定义相机 ios

ios - 在 ios 中调用嵌套的 json 对象

objective-c - NSOperation中的依赖什么时候调用completionBlock

ios - OCMock 期望在另一个方法中调用一个方法