前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >iOS文件上传的几种形式

iOS文件上传的几种形式

作者头像
码客说
发布于 2019-10-22 06:29:53
发布于 2019-10-22 06:29:53
5.5K00
代码可运行
举报
文章被收录于专栏:码客码客
运行总次数:0
代码可运行

前言

做文件上传功能 有两种方式

一种是HTTP方式,另一种Socket方式 但是HTTP方式不能上传大文件

HTTP方式又有两种

一种是二进制流上传 一种是multipart/form-data形式

HTTP方式

二进制流不能附加其他的参数 multipart/form-data形式可以附加其他参数

平常我们提交表单时 RequestContent-Type为如下所示

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Content-Type: application/x-www-form-urlencoded

如果我们上传的表单中有文件 我们会设置表单enctype="multipart/form-data" 这时提交时RequestContent-Type为如下所示

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Content-Type: multipart/form-data; boundary=alamofire.boundary.9b2bf38bcb25c57e

另一种文件上传RequestContent-Type为如下所示

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Content-Type: application/octet-stream

用Alamofire进行HTTP上传

方式一 (multipart/form-data)

上传可以附带其他参数 但是这种方式没法得到上传进度

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//上传文件
static func uploadImage(url:String,parameters:[String:AnyObject],imagePath:NSURL,fileParName:String){
Alamofire.upload(
    .POST,
    url,
    multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(fileURL: imagePath, name: fileParName)
        // 这里就是绑定参数的地方
        for (key, value) in parameters {
            if(value.stringValue != nil){
                multipartFormData.appendBodyPart(data: value.stringValue.dataUsingEncoding(NSUTF8StringEncoding)!, name: key);
            }
        }
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    }
)
方式二 (二进制流)

可以获取上传进度的方式 但是没法附带其他参数

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Alamofire.upload(.POST, "https://httpbin.org/post", file: imagePath)
    .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
        dispatch_async(dispatch_get_main_queue()) {
            print("Total bytes written on main queue: \(totalBytesWritten)")
        }
    }
    .validate()
    .responseJSON { response in
        debugPrint(response)
}

所以例如设置用户头像等就用第一种方式 要是做文件上传就必须用第二种方式 第二种方式也能控制暂停、继续、停止等操作

用AFNetworking进行HTTP上传

方式一
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
static func uploadFile(url:String,parameters:[String:AnyObject]?,data:NSData,callBack:(success:Bool,operation:AFHTTPRequestOperation!,responseObject: AnyObject?,error:NSError?) -> Void ,progressBlock:(bytesWritten:UInt,totalBytesWritten:Int64,totalBytesExpectedToWrite:Int64) -> Void) -> AFHTTPRequestOperation {
    let manager = AFHTTPRequestOperationManager();
    let operation:AFHTTPRequestOperation? = manager.POST(url, parameters: parameters, constructingBodyWithBlock: {
        (formData:AFMultipartFormData!) -> Void in
            formData.appendPartWithFileData(data, name: "file", fileName: "shenfenzheng.jpg", mimeType: "image/jpeg");
        }, success: {
            (operation:AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
            callBack(success: true, operation: operation, responseObject: responseObject, error: nil)
        }, failure: {
            (operation:AFHTTPRequestOperation?,error:NSError?) -> Void in
            callBack(success: false, operation: operation, responseObject: nil, error: error);
    })
    operation?.setUploadProgressBlock(progressBlock);
    return operation!;
}

调用方式

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
let operation = uploadFile("", parameters: nil, data: NSData(), callBack: { (success, operation, responseObject, error) in
    
}) { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
    
}
operation.start();
方式二

这种方式要使用KVO来获取进度,个人不推荐 因为如果同时上传多个文件时进度处理起来会比较麻烦

Swift代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func uploadFile2(data:NSData){
    let parameters:[String:AnyObject] = [
        "userId": "123"
    ];
    let request = AFHTTPRequestSerializer().multipartFormRequestWithMethod("POST", URLString: "http://example.com/upload", parameters: parameters, constructingBodyWithBlock: { (formData) in
        formData.appendPartWithFileData(data, name: "file", fileName: "shenfenzheng.jpg", mimeType: "image/jpeg");
        }, error: nil)
    let manager = AFURLSessionManager(sessionConfiguration: NSURLSessionConfiguration.defaultSessionConfiguration())
    var progress: NSProgress?;
    _ = manager.uploadTaskWithStreamedRequest(request, progress: &progress) { (response, responseObject, error) in
            progress?.removeObserver(self, forKeyPath: "fractionCompleted", context: nil) 
    }
    progress?.addObserver(self, forKeyPath: "fractionCompleted", options: NSKeyValueObservingOptions.Initial, context: nil)
}
    
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if(keyPath == "fractionCompleted"){
        let progress: NSProgress = object as! NSProgress;
        print("progress: \(progress.fractionCompleted)")
    }
}

ObjC代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
    } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"%@ %@", response, responseObject);
    }
}];

[uploadTask resume];

大文件上传

目前考虑到WEB端只能用HTTP方式,所以我用的是HTTP分片上传

方式一 HTTP形式

上面说了 大文件上传需要用Socket 其实用HTTPmultipart/form-data形式也可以

原理就是 上传时把文件进行切片 提交时除了文件data 同时传入 总片数 当前是第几片 服务端得到所有的数据片后合并数据

方式二 Socket形式

Socket上传时 如果是大文件也是要进行分片的

上传下载客户端

上传

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (void)viewDidLoad

{
    [super viewDidLoad];
    self.socketClient = [[AsyncSocket alloc] initWithDelegate:self];
    [self.socketClient connectToHost:@"192.168.1.188" onPort:8000 withTimeout:-1 error:Nil];
    
    // 发送数据
    NSLog(@"发送数据");
    NSString *headerString = [NSString stringWithFormat:@"upload&&%@&&%d",self.file.fileName,self.file.fileLength];
    NSMutableData *allData = [MXUtils getAllDataByHeaderString:headerString];
    NSData *fileData = [NSData dataWithContentsOfFile:self.file.filePath];
    NSLog(@"%d",self.file.fileLength);
    [allData appendData:fileData];
    [self.socketClient writeData:allData withTimeout:-1 tag:0];
    self.labelUploadInfo.text = [NSString stringWithFormat:@"上传%@",self.file.fileName];
}

-(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"上传成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil, nil];
    [alert show];
}

获取下载列表是通过互相发送消息,从服务端把文件对象(也就是文件在服务端的绝对路径)归档发送到客户端,然后在客户端反归档获取文件列表

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (void)viewDidLoad

{

    [super viewDidLoad];
    self.socketClient = [[AsyncSocket alloc] initWithDelegate:self];
    [self.socketClient connectToHost:@"192.168.1.188" onPort:8000 withTimeout:-1 error:Nil];
    NSString *headerString = @"downList&& &&";
    NSMutableData *allData = [MXUtils getAllDataByHeaderString:headerString];
    [self.socketClient writeData:allData withTimeout:-1 tag:0];
    [self.socketClient readDataWithTimeout:-1 tag:0];
}

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    // 反归档
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    self.filePathArray = [unarchiver decodeObjectForKey:@"downlist"];
    NSLog(@"%@",self.filePathArray);
    [self.tableView reloadData];
}

下载是通过列表中的文件路径发送给服务端,然后服务端根据其路径找到文件返回去

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (void)viewDidLoad

{
    [super viewDidLoad];
    self.fileData = [NSMutableData data];
    self.socketClient = [[AsyncSocket alloc] initWithDelegate:self];
    [self.socketClient connectToHost:@"192.168.1.188" onPort:8000 withTimeout:-1 error:Nil];

    // 发送数据
    NSLog(@"发送数据");
    NSString *headerString = [NSString stringWithFormat:@"download&&%@&&",self.file.filePath];
    NSMutableData *allData = [MXUtils getAllDataByHeaderString:headerString];
    NSLog(@"%d",self.file.fileLength);
    [self.socketClient writeData:allData withTimeout:-1 tag:0];
    self.labelDownload.text = [NSString stringWithFormat:@"下载%@",self.file.fileName];
    [self.socketClient readDataWithTimeout:-1 tag:0];
}

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    [self.fileData appendData:data];
    if (self.fileData.length == self.file.fileLength) {
        [self.fileData writeToFile:[@"/Users/tarena/yz/第三阶段(高级UI)/day11/download" stringByAppendingPathComponent:self.file.fileName] atomically:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下载成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil, nil];
        [alert show];
    }

    [self.socketClient readDataWithTimeout:-1 tag:0];
}
上传下载服务端
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (void)viewDidLoad

{
    [super viewDidLoad];
    self.socketServer = [[AsyncSocket alloc] initWithDelegate:self];
    [self.socketServer acceptOnPort:8000 error:Nil];
}

-(void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
    NSLog(@"通道");
    self.socketNew = newSocket;
}

-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
    NSLog(@"连接成功%@",host);
    self.host = host;
    [self.socketNew readDataWithTimeout:-1 tag:0];
}

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSLog(@"读取数据成功");
    // 判断是否有消息头
    NSData *headerData = [data subdataWithRange:NSMakeRange(0, 100)];
    NSString *headerString = [[NSString alloc] initWithData:headerData encoding:NSUTF8StringEncoding]; // 消息头
    if (headerString && [headerString componentsSeparatedByString:@"&&"].count == 3) {
        NSLog(@"有消息头");
        NSArray *headerArray = [headerString componentsSeparatedByString:@"&&"];
        NSString *type = headerArray[0];
        if ([type isEqualToString:@"upload"]) {
            self.allData = [NSMutableData data];
            self.fileName = headerArray[1];
            self.fileLength = [headerArray[2] intValue];
            NSData *subData = [data subdataWithRange:NSMakeRange(100, data.length - 100)];
            [self.allData appendData:subData];
            self.labelUploadInfo.text = [NSString stringWithFormat:@"%@在上传%@文件",self.host,self.fileName];
            self.progressUpload.progress = self.allData.length * 1.0 / self.fileLength;
        }else if([type isEqualToString:@"downList"]){
            NSData *data = [MXUtils getFilePathArrayDataByDirectoryPath:@""];
            [self.socketNew writeData:data withTimeout:-1 tag:0];
        }else if([type isEqualToString:@"download"]){
            NSString *filePath = headerArray[1];
            NSData *data = [NSData dataWithContentsOfFile:filePath];
            [self.socketNew writeData:data withTimeout:-1 tag:0];
        }

    }else{

        [self.allData appendData:data];
        self.progressUpload.progress = self.allData.length * 1.0 / self.fileLength;

    }

    if (self.allData.length == self.fileLength) {
        NSString *path = [@"" stringByAppendingPathComponent:self.fileName];
        NSLog(@"写到%@",path);
        [self.allData writeToFile:path atomically:YES];
    }
    [self.socketNew readDataWithTimeout:-1 tag:0];

}

把消息头存进要发送的数据中 并且固定占用多少字节

使用网络需要导入CFNetwork.framework框架

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-06-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
mysql explain用法和结果的含义
explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。
ydymz
2018/09/10
2.2K0
令仔学MySql系列(一)----explain详解
explain显示了MySQL如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。下面是一个例子:
令仔很忙
2018/09/14
9080
令仔学MySql系列(一)----explain详解
Mysql EXPLAIN 实战
如果是空的,没有相关的索引。这时要提高性能,可通过 检验WHERE子句,看是否引用某些字段,或者检查字段不是适合索引。
收心
2022/01/19
1.1K0
Mysql EXPLAIN 实战
Mysql中explain用法和结果字段的含义介绍
指出MySQL能使用哪个索引在表中找到记录,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询使用
全栈程序员站长
2022/07/19
6650
mysql explain用法和结果的含义
table 输出的行所引用的表 type 联接类型。下面给出各种联接类型,按照从最佳类型到最坏类型进行排序:
yaphetsfang
2020/07/30
2.2K0
普通程序员必须掌握的SQL优化技巧
不管是工作中,还是面试中,基本上都需要搞定一些SQL优化技巧,比如说使用explain查看SQL的执行计划,然后,针对执行计划对SQL进行优化。
田维常
2023/02/27
8790
普通程序员必须掌握的SQL优化技巧
MYSQL EXPLAIN结果详解
SIMPLE(simple):简单SELECT(不使用UNION或子查询)。 PRIMARY(primary):子查询中最外层查询,查询中若包含任何复杂的子部分,最外层的select被标记为PRIMARY。 UNION(union):UNION中的第二个或后面的SELECT语句。 DEPENDENT UNION(dependent union):UNION中的第二个或后面的SELECT语句,取决于外面的查询。 UNION RESULT(union result):UNION的结果,union语句中第二个select开始后面所有select。 SUBQUERY(subquery):子查询中的第一个SELECT,结果不依赖于外部查询。 DEPENDENT SUBQUERY(dependent subquery):子查询中的第一个SELECT,依赖于外部查询。 DERIVED(derived):派生表的SELECT (FROM子句的子查询)。 UNCACHEABLE SUBQUERY(uncacheable subquery):(一个子查询的结果不能被缓存,必须重新评估外链接的第一行)
全栈程序员站长
2022/09/28
2.7K0
MYSQL优化有理有据全分析(面试必备)
在MySQL中可以使用EXPLAIN查看SQL执行计划,用法:EXPLAIN SELECT * FROM tb_item
java进阶架构师
2019/01/23
1.3K0
MYSQL优化有理有据全分析(面试必备)
MySQL优化总结
存储引擎:MySQL中的数据、索引以及其他对象是如何存储的,是一套文件系统的实现。
KEN DO EVERTHING
2019/07/31
1.7K0
MySql知识体系总结(2021版)请收藏!!
MySQL的存储引擎架构将查询处理与数据的存储/提取相分离。下面是MySQL的逻辑架构图:
IT大咖说
2021/07/19
1.3K0
实战讲解MySQL执行计划,面试官当场要了我
SELECT标识符。这是查询中SELECT的序列号,表示查询中执行select子句或者操作表的顺序。如果该行引用其他行的并集结果,则该值可为NULL。
JavaEdge
2021/02/22
1.3K0
实战讲解MySQL执行计划,面试官当场要了我
实战讲解MySQL的expain执行计划,面试官当场要了我
SELECT标识符。这是查询中SELECT的序列号,表示查询中执行select子句或者操作表的顺序。如果该行引用其他行的并集结果,则该值可为NULL。
JavaEdge
2022/11/30
8070
实战讲解MySQL的expain执行计划,面试官当场要了我
mysql explain ref null_MySQL Explain详解[通俗易懂]
分别是id,select_type,table、type,partitions,possible_keys,key,key_len,ref,rows,Extra,下面对这些字段出现的可能进行解释:
全栈程序员站长
2022/10/02
1.9K0
MySQL中SQL执行计划详解
MySQL执行计划是sql语句经过查询优化器后,查询优化器会根据用户的sql语句所包含的字段和内容数量等统计信息,选择出一个执行效率最优(MySQL系统认为最优)的执行计划,然后根据执行计划,调用存储引擎提供的接口,获取数据。
星哥玩云
2022/08/17
3.3K0
MySQL中SQL执行计划详解
手把手教你彻底理解MySQL的explain关键字
数据库是程序员必备的一项基本技能,基本每次面试必问。对于刚出校门的程序员,你只要学会如何使用就行了,但越往后工作越发现,仅仅会写sql语句是万万不行的。写出的sql,如果性能不好,达不到要求,可能会阻塞整个系统,那对于整个系统来讲是致命的。
秃头哥编程
2021/06/10
1.6K0
手把手教你彻底理解MySQL的explain关键字
MySQL慢查询优化 EXPLAIN详解
DEPENDENT UNION:连接查询中的第2个或后面的SELECT语句,取决于外面的查询;
Java帮帮
2018/12/24
1.3K0
mysql explain 详解
指出MySQL能使用哪个索引在表中找到记录,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询使用(该查询可以利用的索引,如果没有任何索引显示 null)
仙士可
2022/05/26
9060
mysql explain 详解
全网最全 | MySQL EXPLAIN 完全解读
EXPLAIN作为MySQL的性能分析神器,读懂其结果是很有必要的,然而我在各种搜索引擎上竟然找不到特别完整的解读。都是只有重点,没有细节(例如type的取值不全、Extra缺乏完整的介绍等)。
用户1516716
2020/07/16
1.8K0
【MySQL】执行计划 explain 及 一条select语句在MySQL中的奇幻之旅
重要字段(我个人认为的)再释义: id:这列就是查询的编号,如果查询语句中没有子查询或者联合查询这个标识就一直是1。如存在子查询或者联合查询这个编号会自增。
看、未来
2021/12/20
1.2K0
【MySQL】执行计划 explain 及 一条select语句在MySQL中的奇幻之旅
Mysql Explain的主要字段
3. table         查询的表名。 4. type(重要)显示查询使用了何种类型。         从最好到最差的连接类型依次为: system,const, eq_ref ,ref,fulltext,ref_or_null,index_merge, unique_subquery,index_subquery,range,index,ALL
编程张无忌
2022/05/25
1.4K0
Mysql Explain的主要字段
相关推荐
mysql explain用法和结果的含义
更多 >
LV.0
这个人很懒,什么都没有留下~
目录
  • 前言
  • HTTP方式
    • 用Alamofire进行HTTP上传
      • 方式一 (multipart/form-data)
      • 方式二 (二进制流)
    • 用AFNetworking进行HTTP上传
      • 方式一
      • 方式二
  • 大文件上传
    • 方式一 HTTP形式
    • 方式二 Socket形式
      • 上传下载客户端
      • 上传下载服务端
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档