标签:
- (void)upload { //1.确定请求路径 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/upload"]; //2.创建一个可变的请求对象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //3.设置请求方式为POST request.HTTPMethod = @"POST"; //4.设置请求头 NSString *filed = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",Kboundary]; [request setValue:filed forHTTPHeaderField:@"Content-Type"]; //5.设置请求体 NSMutableData *data = [NSMutableData data]; //5.1 文件参数 /* --分隔符 Content-Disposition:参数 Content-Type:参数 空行 文件参数 */ [data appendData:[[NSString stringWithFormat:@"--%@",Kboundary] dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:KnewLine]; [data appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"test.png\"" dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:KnewLine]; [data appendData:[@"Content-Type: image/png" dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:KnewLine]; [data appendData:KnewLine]; [data appendData:KnewLine]; UIImage *image = [UIImage imageNamed:@"test"]; NSData *imageData = UIImagePNGRepresentation(image); [data appendData:imageData]; [data appendData:KnewLine]; //5.2 非文件参数 /* --分隔符 Content-Disposition:参数 空行 非文件参数的二进制数据 */ [data appendData:[[NSString stringWithFormat:@"--%@",Kboundary] dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:KnewLine]; [data appendData:[@"Content-Disposition: form-data; name=\"username\"" dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:KnewLine]; [data appendData:KnewLine]; [data appendData:KnewLine]; NSData *nameData = [@"wendingding" dataUsingEncoding:NSUTF8StringEncoding]; [data appendData:nameData]; [data appendData:KnewLine]; //5.3 结尾标识 //--分隔符-- [data appendData:[[NSString stringWithFormat:@"--%@--",Kboundary] dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:KnewLine]; request.HTTPBody = data; //6.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * __nullable response, NSData * __nullable data, NSError * __nullable connectionError) { //7.解析服务器返回的数据 NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]); }]; }
//如果想要及时拿到该数据,那么可以发送一个同步请求 - (NSString *)getMIMEType { NSString *filePath = @"/Users/HMJ/Desktop/备课/其它/swift.md"; NSURLResponse *response = nil; [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]] returningResponse:&response error:nil]; return response.MIMEType; } //对该文件发送一个异步请求,拿到文件的MIMEType - (void)MIMEType { // NSString *file = @"file:///Users/HMJ/Desktop/test.png"; [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:@"/Users/HMJ/Desktop/test.png"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * __nullable response, NSData * __nullable data, NSError * __nullable connectionError) { // response.MIMEType NSLog(@"%@",response.MIMEType); }]; }
//注意:需要依赖于框架MobileCoreServices - (NSString *)mimeTypeForFileAtPath:(NSString *)path { if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) { return nil; } CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL); CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType); CFRelease(UTI); if (!MIMEType) { return @"application/octet-stream"; } return (__bridge NSString *)(MIMEType); }
标签:
原文地址:http://www.cnblogs.com/HMJ-29/p/4943945.html