NSString *ext = @"";
if (UIImagePNGRepresentation(image)) {//返回为png图像。
ext = @"png";
}else {//返回为JPEG图像。
ext = @"jpeg";
}
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:self.uploadAliInfo.Key forKey:@"Key"];
[parameters setValue:self.uploadAliInfo.OSSAccessKeyId forKey:@"OSSAccessKeyId"];
[parameters setValue:self.uploadAliInfo.Policy forKey:@"Policy"];
[parameters setValue:self.uploadAliInfo.Signature forKey:@"Signature"];
[parameters setValue:self.uploadAliInfo.Success_action_status forKey:@"Success_action_status"];
[parameters setValue:[NSString stringWithFormat:@"temp.%@", ext] forKey:@"file"];
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
[session.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
session.responseSerializer = [AFHTTPResponseSerializer serializer];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"application/xml", nil];
NSURLSessionDataTask *uploadTask = [session POST:[NSString stringWithFormat:@"http:%@", self.uploadAliInfo.Url] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[formData appendPartWithFileData :imageData name:@"temp" fileName:[NSString stringWithFormat:@"temp.%@", ext] mimeType:@"multipart/form-data"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
iOS multipart/form-data表单上传图片到服务器
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 我以为一直比较难弄,在网上查阅了一些资料,看的都比较懵,自己总结了一下。高手勿喷。 直接上代码 //创建属性 @p...
- 1 两年过去,南枫和蔡筱在美术领悟都获得了不少成绩。南枫大大小小获得许多奖项,也拿到了国外美术展的邀请函,但她没有...