檔案的上傳

來源:互聯網
上載者:User

標籤:web   field   請求   get   tag   步驟   __bridge   格式   mic   

  • 10.1 檔案上傳步驟

      (1)確定請求路徑  (2)根據URL建立一個可變的請求對象  (3)佈建要求對象,修改請求方式為POST  (4)佈建要求頭,告訴伺服器我們將要上傳檔案(Content-Type)  (5)佈建要求體(在請求體中按照既定的格式拼接要上傳的檔案參數和非檔案參數等資料)      001 拼接檔案參數      002 拼接非檔案參數      003 添加結尾標記  (6)使用NSURLConnection sendAsync發送非同步請求上傳檔案  (7)解析伺服器返回的資料
  • 10.2 檔案上傳佈建要求體的資料格式

      //請求體拼接格式  //分隔字元:----WebKitFormBoundaryhBDKBUWBHnAgvz9c  //01.檔案參數拼接格式   --分隔字元   Content-Disposition:參數   Content-Type:參數   空行   檔案參數  //02.非檔案拼接參數   --分隔字元   Content-Disposition:參數   空行   非檔案的位元據  //03.結尾標識  --分隔字元--
  • 10.3 檔案上傳相關代碼

- (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]);    }];}
  • 10.4 如何獲得檔案的MIMEType類型

(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);}

檔案的上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.