【问题标题】:video posting to server through http post method in iOS在 iOS 中通过 http post 方法将视频发布到服务器
【发布时间】:2017-05-23 08:59:09
【问题描述】:

请完善我编写的使用 HTTP 发布方法将视频发布到服务器的代码。

服务器被击中,但响应显示为 null 我不明白出了什么问题

 - (IBAction)choose:(id)sender {

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    picker.delegate = self;

    picker.allowsEditing = YES;

    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

    [self presentViewController:picker animated:YES completion:NULL];

}



      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


NSString *type = [info objectForKey:UIImagePickerControllerMediaType];


  if ([type isEqualToString:(NSString *)kUTTypeVideo] ||

    [type isEqualToString:(NSString *)kUTTypeMovie])

   {


    self.videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

}

[picker dismissViewControllerAnimated:YES completion:NULL];
}

 - (IBAction)upload:(id)sender {

    //NSString * str = @"content";

  //    [str stringByAppendingString:self.url];

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://mobile.supportservice.co.in/bbgapp.asmx/Stream" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    //NSString * str = [[NSBundle mainBundle] pathForResource:@"amyVideo" ofType:@"png"];

    NSLog(@"------------------>%@",self.videoURL);


    [formData appendPartWithFileURL:[NSURL fileURLWithPath:self.videoURL] name:@"file" fileName:@"myVideo.m4v" mimeType:@"mp4/m4v" error:nil];

} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;

uploadTask = [manager

              uploadTaskWithStreamedRequest:request

              progress:^(NSProgress * _Nonnull uploadProgress) {

                  // This is not called back on the main queue.

                  // You are responsible for dispatching to the main queue for UI updates
              dispatch_async(dispatch_get_main_queue(), ^{

                      //Update the progress view

                      [self.progressView setProgress:uploadProgress.fractionCompleted];

                  });

              }

              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

                  if (error) {

                      NSLog(@"Error: %@", error);

                  } else {


  NSLog(@"----------------------> %@ %@", response, responseObject);

                  }

              }];

[uploadTask resume];
}
  @end

这是我的output

【问题讨论】:

    标签: ios objective-c web-services post


    【解决方案1】:

    上传/下载视频或图片的最佳方式是使用AFNetworking 框架。对于上传视频,请使用以下代码。

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" 
    URLString:@"http://exampleurl.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
     [formData appendPartWithFileData:videoData name:@"file" fileName:@"filename.mov" mimeType:@"video/quicktime"];
    } error:nil];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager
                  uploadTaskWithStreamedRequest:request
                  progress:^(NSProgress * _Nonnull uploadProgress) {
                      // This is not called back on the main queue.
                      // You are responsible for dispatching to the main queue for UI updates
                      dispatch_async(dispatch_get_main_queue(), ^{
                          //Update the progress view
                          [progressView setProgress:uploadProgress.fractionCompleted];
                      });
                  }
                  completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                      if (error) {
                          NSLog(@"Error: %@", error);
                      } else {
                          NSLog(@"%@ %@", response, responseObject);
                      }
                  }];
    
    [uploadTask resume];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多