开发者

Issue with uploading a video file using HTTP Post from iPhone app to server:

I am trying to upload a .3gp video file into my server using HTTP post method from my iPhone app to my server. 3gp video file is available in my project resource. I use the following code for that,

-(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 da开发者_JAVA百科ta];

    [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];

}

This code doesn't do anything.

Could someone guide me what's wrong?

UPDATED:

I saw an example from the link -> iphone.zcentric.com/page/2 there are using "iphone.zcentric.com/test-upload.php"; PHP to upload and in my code i use JSP "115.111.27.206:8081/vblo/upload.jsp"; to upload to my server. Is this anything wrong here?

Thanks.


I suggest the following sample code. The multipart is not required if you send one single body - in this case the video. I would recommend binary encoding instead of any other character encoding for speed and preserve binary data integrity.

  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];

Good luck!


As it looks like you are faking being a form I would recommend using ASIFormDataRequest

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


Maybe you should have a look at the response and - maybe - the Error:

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜