標籤:web field 請求 get tag 步驟 __bridge 格式 mic
- (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]); }];}
(1)直接對該對象發送一個非同步網路請求,在回應標頭中通過response.MIMEType拿到檔案的MIMEType類型
//如果想要及時拿到該資料,那麼可以發送一個同步請求- (NSString *)getMIMEType{ NSString *filePath = @"/Users/文頂頂/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/文頂頂/Desktop/test.png"; [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:@"/Users/文頂頂/Desktop/test.png"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * __nullable response, NSData * __nullable data, NSError * __nullable connectionError) { // response.MIMEType NSLog(@"%@",response.MIMEType); }];}
(2)通過UTTypeCopyPreferredTagWithClass方法
//注意:需要依賴於架構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);}
檔案的上傳