2016-11-24 142 views
1

我使用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]; 
+0

這裏有什麼問題嗎? – KKRocks

+0

服務器返回錯誤,但在圖像上工作 – williamliao

回答

0

*

Try this 
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@%@",BASEURL,url] parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
      if (videoData) { 
       [formData appendPartWithFileData: videoData name:@"video_file" fileName:@"video_file.mp4" mimeType:@"video/mp4"]; 

       // you file to upload 
      } 
     }error:nil]; 

     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
     manager.responseSerializer = [AFJSONResponseSerializer serializer]; 

     NSURLSessionUploadTask *uploadTask; 
     uploadTask = [manager 
         uploadTaskWithStreamedRequest:request 
         progress:^(NSProgress * _Nonnull uploadProgress) { 
          dispatch_async(dispatch_get_main_queue(), ^{ 
          }); 
         } 
         completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 
          if (error) { 
           NSLog(@"Error: %@", [[NSString alloc]initWithData:[[error valueForKey:@"userInfo"] valueForKey:@"com.alamofire.serialization.response.error.data"] encoding:NSUTF8StringEncoding]); 

          } else { 
          } 
         }]; 

     [uploadTask resume]; 

*

+0

感謝您的幫助,但對我來說沒有運氣。 – williamliao

+0

服務器仍然無法上傳錯誤 – williamliao

+0

給我錯誤消息。 – KKRocks

0
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
     manager.requestSerializer = [AFJSONRequestSerializer serializer]; 

     AFHTTPRequestOperation *operation = [manager POST:apiType parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData){ 
      [formData appendPartWithFileData:fileData name:@"file" fileName:strFileName mimeType:mimeType]; 

     }success:^(AFHTTPRequestOperation *operation,id responseObject){ 

      NSLog(@"JSON: %@", responseObject); 
      successHandler(operation, responseObject); 
     } 
       failure:^(AFHTTPRequestOperation *operation, NSError *error){ 
        myProgressView.progress = 1.0; 

       }]; 
     }]; 
相關問題