关闭

文件上传及进度显示

标签: 文件上传-进度
117人阅读 评论(0) 收藏 举报
分类:

之前遇到需要显示文件上传时的进度问题,就整理了下资料,没什么复杂的东西,看代码吧。

NSString *urlString = [NSString stringWithFormat:@"%@api/?method=gdb.send_talk",BASEURL];//你的URL

    NSDictionary *dict = @{@"type":[NSString stringWithFormat:@"%d",pvtype],@"content":_textView.text};//需要传的dict,没有写nil

    AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:urlString
                                                                   parameters:dict
                                                    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                                                        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                                                        // 设置时间格式
                                                        formatter.dateFormat = @"yyyyMMddHHmmss";
                                                        NSString *str = [formatter stringFromDate:[NSDate date]];
                                                       //按时间给传的文件命名
                                                            NSString *imageName = [NSString stringWithFormat:@"%@.jpg", str];
                                                            NSString *videoName = [NSString stringWithFormat:@"%@.mp4", str];

                                                            [formData appendPartWithFileData:videoData name:@"video" fileName:videoName mimeType:@"mp4"];//传视频 videoData  视频所在的data
                                                            [formData appendPartWithFileData:image_data name:@"image" fileName:imageName mimeType:@"jpg"];//传图片 imageData 图片所在的data



                                                    }];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                         NSLog(@"Success %@", responseObject);
                                         [self.navigationController popViewControllerAnimated:YES];

                                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"Failure %@", error.description);
                                     }];
    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                        long long totalBytesWritten,
                                        long long totalBytesExpectedToWrite) {

        NSLog(@"传了%.2fMB/共 %.2fMB",totalBytesWritten/1024.0/1024.0,totalBytesExpectedToWrite/1024.0/1024.0);

    }];
    [operation start];
2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4092次
    • 积分:221
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:3篇
    • 译文:0篇
    • 评论:0条