我之前通过执行以下操作将文件上传到预签名网址:
NSData *data = [NSData dataWithContentsOfURL:self.videoURL];
[self.httpSessionManager.requestSerializer setValue:@"video/mp4" forHTTPHeaderField:@"Content-Type"];
[self.httpSessionManager PUT:operation.relativeURLString parameters:@{@"data": data} success:^(NSURLSessionDataTask *task, id responseObject) {
[self handleResponse:responseObject forSuccessfulOperation:operation];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self handleError:error forFailedOperation:operation];
}];
但是跟踪上传进度的需要让我将其更改为:
NSData *data = [NSData dataWithContentsOfURL:self.videoURL];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"PUT" URLString:operation.relativeURLString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data
name:@"data"
fileName:@"video.mp4"
mimeType:@"video/mp4"];
} error:nil];
NSURLSessionDataTask *uploadTask = [self.httpSessionManager uploadTaskWithStreamedRequest:request progress:progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (!error)
{
[self handleResponse:responseObject forSuccessfulOperation:operation];
}
else
{
[self handleError:error forFailedOperation:operation];
}
}];
[uploadTask resume];
这似乎成功上传文件,直到我尝试播放它。它具有正确的文件大小,但文件似乎被破坏,因为它不会播放。我是否误解了如何使用multipartFormRequest?我已经明白使用NSStream或内存映射文件而不是传递NSData是可取的,但据我所知,这不应该是我的问题的原因,而只是性能调整。