【问题标题】:Issue with uploading a video file using HTTP Post from iPhone app to server:使用 HTTP Post 从 iPhone 应用程序上传视频文件到服务器的问题:
【发布时间】:2010-11-24 08:54:46
【问题描述】:

我正在尝试使用 HTTP 发布方法将 .3gp 视频文件从我的 iPhone 应用程序上传到我的服务器。我的项目资源中提供了 3gp 视频文件。为此,我使用以下代码,

-(IBAction)buttonAction
{
    NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @"http://115.111.27.206:8081/vblo/upload.jsp"]];

    [post setHTTPMethod: @"POST"];


    NSString *boundary = [NSString stringWithString:@"---------------------------358734318367435438734347"];

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

    [post addValue:contentType forHTTPHeaderField: @"Content-Type"];


    body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"videofile\"; filename=\"video.3gp\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[[NSBundle mainBundle] pathForResource:@"video" ofType:@"3gp"] 
                      dataUsingEncoding: NSASCIIStringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


    [post setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:post returningResponse:nil error:nil];

    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    // Just to show the response received from server in an alert...
    UIAlertView *statusAlert = [[UIAlertView alloc]initWithTitle:nil message:(NSString *)returnString delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    [statusAlert show];

}

这段代码没有任何作用。

谁能指导我怎么了?

更新:

我从链接中看到了一个示例-> iphone.zcentric.com/page/2 正在使用“iphone.zcentric.com/test-upload.php”; PHP 上传,在我的代码中我使用 JSP "115.111.27.206:8081/vblo/upload.jsp";上传到我的服务器。这里有什么问题吗?

谢谢。

【问题讨论】:

  • 我不建议同步发送视频文件,因为它们通常很大并且用户希望看到某种进度条。

标签: iphone


【解决方案1】:

我建议使用以下示例代码。如果您发送一个单一的正文,则不需要多部分 - 在这种情况下是视频。为了速度和保持二进制数据的完整性,我建议使用二进制编码而不是任何其他字符编码。

  NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @"http://115.111.27.206:8081/vblo/upload.jsp"]];
  [post setHTTPMethod: @"POST"]; 
  [post addValue:@"video/3gpp" forHTTPHeaderField:@"Content-Type"]; 
  body = [[NSData alloc] initWithContentOfFile:[[NSBundle mainBundle] pathForResource:@"video" ofType:@"3gp"]]; 
  [post setHTTPBody:body]; 
  NSData *returnData = [NSURLConnection sendSynchronousRequest:post returningResponse:nil error:nil];
  [post release];

祝你好运!

【讨论】:

    【解决方案2】:

    看起来你是在伪装成一个表单,我建议使用 ASIFormDataRequest

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request addData:videoData withFileName:@"videofile.3gp" andContentType:@"video/3gpp" forKey:@"video"];
    

    【讨论】:

      【解决方案3】:

      也许你应该看看响应和 - 也许 - 错误:

      NSError *error;
      
      NSURLResponse *response;
      
      NSData *returnData = [NSURLConnection sendSynchronousRequest:post 
      returningResponse:response error:error];
      
      if (returnData==nil) {
          /* Edit this or set Breakpoint */
          NSLog(@"Ups... Response: %@  Error: %@",response,error);
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 2023-03-18
      相关资源
      最近更新 更多