美文网首页
iOS 分段上传的方法

iOS 分段上传的方法

作者: 清蘂翅膀的技术 | 来源:发表于2017-11-12 04:56 被阅读0次

    #define XMGBoundary @"GetHTML"

    #define XMGEncode(string) [string dataUsingEncoding:NSUTF8StringEncoding]

    #define XMGNewLine [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]

    #pragma mark - 断点续传(服务器要做设置)

    - (void)update:(NSDictionary*)dict {

    NSDictionary *attriDict = [[NSFileManager defaultManager] attributesOfItemAtPath:[self.filePathURL path] error:nil];

    //循环次数

    NSString * fileSize = attriDict[NSFileSize];

    NSInteger times = fileSize.longLongValue/1000000;

    //开始循环读取

    for (NSInteger index = 0; index < times ; index ++) {

    //创建句柄

    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:[self.filePathURL path]];

    //设置每次句柄偏移量

    [fileHandle seekToFileOffset:1000000 *(index + 1)];

    //获取每次读入data

    NSData *data = [fileHandle readDataOfLength:1000000];

    [self updateVideo:dict Data:data];

    NSLog(@"-----%lu",(unsigned long)data.length);

    if (index == times - 1) {

    //关闭句柄

    [fileHandle closeFile];

    }

    }

    }

    - (void)updateVideo:(NSDictionary*)dict Data:(NSData *)data {

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://********"]];

    request.HTTPMethod = @"POST";

    request.timeoutInterval = 1000;

    // 设置请求头(告诉服务器,这是一个文件上传的请求)

    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", XMGBoundary] forHTTPHeaderField:@"Content-Type"];

    NSLog(@"%@",request);

    // 设置请求体

    NSMutableData *body = [NSMutableData data];

    // 文件参数

    // 分割线

    [body appendData:XMGEncode(@"--")];

    [body appendData:XMGEncode(XMGBoundary)];

    [body appendData:XMGNewLine];

    // 文件参数名

    NSString *fileStr = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"",self.videoName];

    [body appendData:XMGEncode(fileStr)];

    [body appendData:XMGNewLine];

    // 文件的类型

    NSString *typeStr = [NSString stringWithFormat:@"Content-Type: %@",dict[@"contenttype"]];

    [body appendData:XMGEncode(typeStr)];

    [body appendData:XMGNewLine];

    // 文件数据

    [body appendData:XMGNewLine];

    if (data) {

    [body appendData:data];

    } else {

    [body appendData:[NSData dataWithContentsOfFile:[_filePathURL path]]];

    }

    [body appendData:XMGNewLine];

    // 结束标记

    /*

    --分割线--\r\n

    */

    [body appendData:XMGEncode(@"--")];

    [body appendData:XMGEncode(XMGBoundary)];

    [body appendData:XMGEncode(@"--")];

    [body appendData:XMGNewLine];

    NSLog(@"%@",body);

    [[self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    NSLog(@"%@",[[ NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    }] resume];

    }

    相关文章

      网友评论

          本文标题:iOS 分段上传的方法

          本文链接:https://www.haomeiwen.com/subject/jxcnmxtx.html