1. 程式人生 > >文件的上傳

文件的上傳

需要 res option gdi bridge nsstring tab format test

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

文件的上傳