我使用post方法將視頻上傳到服務器。但是有沒有被服務器接收的問題。如何使用AFNetworking從iPhone上傳視頻到服務器
下面是我發佈視頻的代碼。
此代碼爲我工作在上傳圖片,但是當來到視頻獲得失敗
我使用的UIImagePickerController獲得視頻
AVAssetExportSession到轉換器MP4
,當我試圖上傳我的視頻,服務器看不到我的帖子。
但我不明白爲什麼?
請給我一些建議。謝謝。
NSString *boundary = @"---011000010111000001101001";
NSMutableData *body = [NSMutableData data];
NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=---011000010111000001101001",
@"cache-control": @"no-cache"};
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"uid"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"title"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", title] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"note"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", note] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if (videoData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@.mp4; filename=%@.mp4\r\n", @"file", @"video1"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: multipart/form-data; boundary=---011000010111000001101001\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: video/mp4\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:videoData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSError *errorFormAppend;
AFHTTPRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:@"url" parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithHeaders:headers body:body];
NSError *error = errorFormAppend;
NSLog(@"errorFormAppend: %@", error);
// [formData appendPartWithFileData:imageData name:@"file[]" fileName:@"video1.mp4"
} error:nil];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setHTTPBodyStream:[NSInputStream inputStreamWithFileAtPath:[originVideoURL path]]];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:body];
[request addValue:@"video/mp4" forHTTPHeaderField:@"Content-Type"];
[request setTimeoutInterval:60.0];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"multipart/form-data",@"text/html",nil];
[manager.requestSerializer setTimeoutInterval:60.0];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error video upload: %@", error);
} else {
if (200 == [responseObject[@"status_code"] integerValue]) {
NSLog(@"upload Success");
} else {
}
}
}];
[uploadTask resume];
這裏有什麼問題嗎? – KKRocks
服務器返回錯誤,但在圖像上工作 – williamliao