zoukankan      html  css  js  c++  java
  • 网络-文件下载上传网络 NSURLSession

    小文件下载
    如果文件比较小,下载方式会比较多
    直接用NSData的+ (id)dataWithContentsOfURL:(NSURL *)url;
    利用NSURLConnection发送一个HTTP请求去下载
    如果是下载图片,还可以利用SDWebImage框架

    如果是大文件下载,建议使用NSURLSession或者第三方框架


    文件上传的步骤

    设置请求头
    [request setValue:@"multipart/form-data; boundary=分割线" forHTTPHeaderField:@"Content-Type"];
    
    设置请求体
    非文件参数
    --分割线
    
    Content-Disposition: form-data; name="参数名"
    
    
    
    参数值
    
    
    文件参数
    --分割线
    
    Content-Disposition: form-data; name="参数名"; filename="文件名"
    
    Content-Type: 文件的MIMEType
    
    
    
    文件数据
    
    
    
    参数结束的标记
    --分割线--
    

    部分文件的MIMEType

     获得文件的MIMEType

    利用NSURLConnection
    - (NSString *)MIMEType:(NSURL *)url
    {
        // 1.创建一个请求
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        // 2.发送请求(返回响应)
        NSURLResponse *response = nil;
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        // 3.获得MIMEType
        return response.MIMEType;
    }
    C语言API
    + (NSString *)mimeTypeForFileAtPath:(NSString *)path
    {
        if (![[NSFileManager alloc] init] fileExistsAtPath:path]) {
            return nil;
        }
        
        CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
        CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
        CFRelease(UTI);
        if (!MIMEType) {
            return @"application/octet-stream";
        }
        return NSMakeCollectable(MIMEType);
    }

    第三方解压缩框架——ZipArchive

    下载地址:https://github.com/ZipArchive/ZipArchive
    需要引入libz.dylib框架
    导入头文件Main.h
    创建压缩文件
    + (BOOL)createZipFileAtPath:(NSString *)path
               withFilesAtPaths:(NSArray *)paths;
    + (BOOL)createZipFileAtPath:(NSString *)path
        withContentsOfDirectory:(NSString *)directoryPath;

    解压
    + (BOOL)unzipFileAtPath:(NSString *)path
              toDestination:(NSString *)destination

    网络 NSURLSession 

    NSURLSession
    使用步骤
    使用NSURLSession对象创建Task,然后执行Task

    Task的类型

    获得共享的Session
    + (NSURLSession *)sharedSession;
    
    自定义Session
    + (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration delegate:(id <NSURLSessionDelegate>)delegate delegateQueue:(NSOperationQueue *)queue;

    NSURLSessionTask

    常见方法
    - (void)suspend; // 暂停
    - (void)resume; // 恢复
    - (void)cancel; // 取消
    @property (readonly, copy) NSError *error; // 错误
    @property (readonly, copy) NSURLResponse *response; // 响应

    NSURLSessionDownloadTask

    常见方法
    - (void)cancelByProducingResumeData:(void (^)(NSData *resumeData))completionHandler; // 取消任务



    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    redis被攻击,怎么预防
    Redis3.2.12单节点安装
    Linux安装Redis、后台运行、系统自启动
    Redis 密码设置和查看密码
    Redis protected-mode属性解读
    SpringCloud(9)----mysql实现配置中心
    SpringCloud(8)----zuul权限校验、接口限流
  • 原文地址:https://www.cnblogs.com/laugh/p/6593785.html
Copyright © 2011-2022 走看看