文章预读:
iOS里实现multipart/form-data格式上传文件
iOS里实现multipart/form-data格式上传文件
项目中设置
NSString *kHttpRequestHeadContentTypeValueMultipart = @"multipart/form-data; boundary= fgejigjiejigest";
NSString *kHttpRequestHeadContentTypeKey = @"Content-Type";
NSString *kHttpRequestHeadBoundaryValue = @"fgejigjiejigest";
NSString *kHttpRequestContentDisposition = @"Content-Disposition: form-data";
NSString *urlString = @"Your Address";
NSURL *url = [NSURL URLWithString:urlString];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
mutableRequest.HTTPMethod = @"POST";
[mutableRequest addValue:kHttpRequestHeadContentTypeValueMultipart forHTTPHeaderField:kHttpRequestHeadContentTypeKey];
NSString *body = [NSString stringWithFormat:@"--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"Identification", @"Identification对应的字段值"];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"appuserIdStr",@"appuserIdStr对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"arrivalTime",@"arrivalTime对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"billCommodityStr",@"billCommodityStr对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"billTime",@"billTime对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"day",@"day对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"freight",@"freight对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"isStages",@"isStages对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"billByStagesStr",@"billByStagesStr对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"logisticsNum",@"logisticsNum对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"mobilePhone",@"mobilePhone对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"remarks",@"remarks对应的字段值"]];
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"state",@"state对应的字段值"]];
//这里是要上传图片了,注意上传图片你的格式
body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"; filename=\"%@\" Content-Type=image/jpeg\r\n\r\n", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"files", @"WechatIMG1557.jpeg"]];
NSMutableData *data = [NSMutableData data];
[data appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"WechatIMG1557.jpeg" ofType:nil];
NSData *imageData = [[NSData alloc]initWithContentsOfFile:imagePath];
[data appendData:imageData];
[data appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", kHttpRequestHeadBoundaryValue] dataUsingEncoding:NSUTF8StringEncoding]];
mutableRequest.HTTPBody = data;
[mutableRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:mutableRequest fromData:nil completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSError *errorError;
id dataObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&errorError];
if ([dataObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *dict = (NSDictionary *)dataObject;
NSInteger code = [[dict valueForKey:@"code"] integerValue];
NSDictionary *resultDict = [dict valueForKey:@"result"];
if (code == 200 && resultDict.count) {
[SVProgressHUD showSuccessWithStatus:@"成功"];
NSLog(@"%@",dataObject);
//进入相应的界面处理中
dispatch_async(dispatch_get_main_queue(), ^{
//回调或者说是通知主线程刷新
});
}else{
[SVProgressHUD showErrorWithStatus:@"失败"];
}
}else{
[SVProgressHUD showSuccessWithStatus:@"失败"];
}
}else{
NSLog(@"%@",error);
[SVProgressHUD showErrorWithStatus:@"由于网络原因发送失败,请调整网络后重试"];
}
//NSLog(@"%@",response);
}];
[task resume];
注意:
①还有一个是上传文件的格式,上面只弄了一个上传图片的
②上面是很傻瓜的办法,但是也可以总结一下使用For循环来添加body
以上!!!