【问题标题】:Can not upload video with multi part POST in AFNETWORKING on iOS无法在 iOS 上的 AFNETWORKING 中使用多部分 POST 上传视频
【发布时间】:2014-02-10 12:18:05
【问题描述】:

我尝试使用下面的代码在AFnetworking 中上传带有Multi part form POST 的视频,但是在上传时,发送的视频大约 80% 已损坏。这是我的代码:

    -(void) uploadVideoAPI: (NSString*) emailStr andSumOfFiles: (NSString*) sumSizeFile  andVideoNams:(NSMutableArray*) videoNameArr andUpFile :(NSMutableArray *) videoDataArray
{
    NSURL *url = [NSURL URLWithString:@"http://myserver.com];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: url] ;

    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {
        [formData appendPartWithFormData:[emailStr dataUsingEncoding:NSUTF8StringEncoding]
                                    name:@"emailStr"]; //parametters1

        [formData appendPartWithFormData:[sumSizeFile dataUsingEncoding:NSUTF8StringEncoding] name:@"sumSizeFile"];//parametters 2

        for(int i=0;i<[videoDataArray count];i++)
        {
            NSString * videoName = [videoNameArr objectAtIndex:i];
            NSData *videoData = [videoDataArray objectAtIndex:i];
            [formData appendPartWithFileData:videoData
                                        name:@"videos"
                                    fileName:videoName mimeType:@"video/quicktime"];
        }

    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

    }];
    [httpClient enqueueHTTPRequestOperation:operation];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Upload Complete");
    }
                                     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"error: %@", operation.responseString);
                                         NSLog(@"%@",error);
                                     }];
    [operation start];


}

我的代码有问题吗?请给我一些建议。提前致谢

【问题讨论】:

标签: ios objective-c upload afnetworking multipartform-data


【解决方案1】:

我建议您对文件使用appendPartWithFileURL 而不是appendPartWithFormData,以避免内存问题(想象大数据,如视频或压缩数据文件)。

我使用这样的东西:

// Create request
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {
    // Important!! : file path MUST BE real file path (so -> "file://localhost/.../../file.txt") so i use [NSURL fileURLWithPath:]
    NSError* err;
    [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePathToUpload] name:[fileInfo objectForKey:@"fileName"] error:&err];
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2014-03-19
    • 2013-03-02
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多