//分隔符
#define YFBoundary @"AnHuiWuHuYungFan"
//换行
#define YFEnter [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]
//NSString转NSData
#define YFEncode(string) [string dataUsingEncoding:NSUTF8StringEncoding]
typedef void (^XTOriginResultBlock)(NSInteger status, id _Nullable result);
#pragma mark --uploadData
- (void)netWorkUrlUserPost:(NSString *)url UploadImage:(UIImage *)image FileName:(NSString *)fileName Block:(XTOriginResultBlock)block {
//1、确定URL
NSURL *url2 = [NSURL URLWithString:url];
//2、确定请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url2];
//3、设置请求头
NSString *head = [NSString stringWithFormat:@"multipart/form-data;boundary=%--@", YFBoundary];
[request setValue:head forHTTPHeaderField:@"Content-Type"];
//4、设置请求方式,上传时必须是Post请求
request.HTTPMethod = @"POST";
//5、创建NSURLSession
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
//6、获取上传的数据
NSData *uploadData = [self getData:image withFileName:fileName];
//7、创建上传任务 上传的数据来自getData方法
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:uploadData completionHandler:^(NSData *responseObject, NSURLResponse *response, NSError *error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
if (dic == nil) {
NSDictionary *errorInfo = @{
@"status" : @(200),
@"url" : url,
@"data" : @{},
@"headers" : @{}
};
block(1,errorInfo);
} else {
block(0,dic);
}
if (error) {
block(-1,[error userInfo].description);
[self notifiTologin:error];
}
[SVProgressHUD dismiss];
}];
//8、执行上传任务
[task resume];
}
/**
* 设置请求体
*
* @return 请求体内容
*/
-(NSData *)getData:(UIImage *)withImage withFileName:(NSString *)fileName {
DEBUGLOG(@"%@",fileName);
NSMutableData *data = [NSMutableData data];
//1、开始标记
//--
[data appendData:YFEncode(@"--")];
//boundary
[data appendData:YFEncode(YFBoundary)];
//换行符
[data appendData:YFEnter];
//文件参数名 Content-Disposition: form-data; name="myfile"; filename="wall.jpg"
NSString *uploadFileName = [[@"Content-Disposition:form-data; name=\"myfile\"; filename=\"" stringByAppendingString:fileName] stringByAppendingString:@"\""];
[data appendData:YFEncode(uploadFileName)];
//换行符
[data appendData:YFEnter];
//Content-Type 上传文件的类型 MIME
[data appendData:YFEncode(@"Content-Type:image/jpeg")];
//换行符
[data appendData:YFEnter];
//换行符
[data appendData:YFEnter];
//2、上传的文件数据
//图片数据 并且转换为Data
UIImage *image = withImage;
NSData *imagedata = UIImageJPEGRepresentation(image, 1.0);
[data appendData:imagedata];
//换行符
[data appendData:YFEnter];
//3、结束标记
//--
[data appendData:YFEncode(@"--")];
//boundary
[data appendData:YFEncode(YFBoundary)];
//--
[data appendData:YFEncode(@"--")];
//换行符
[data appendData:YFEnter];
return data;
}
//使用NSURLSessionTaskDelegate代理监听上传进度
#pragma mark --NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didSendBodyData:(int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
// 计算进度
float progress = (float)totalBytesSent / totalBytesExpectedToSend;
DEBUGLOG(@"进度 %f",progress);
[SVProgressHUD showProgress:progress status:@"文件上传中,请稍后"];
}
NSURLSession 上传图片带进度
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...