我正在尝试使用从我的iPhone应用程序到我的服务器的HTTP post方法将.3gp视频文件上传到我的服务器。我的项目资源中提供了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”;上传到我的服务器。这有什么问题吗?
感谢。
答案 0 :(得分:3)
我建议使用以下示例代码。如果您发送一个单独的主体 - 在这种情况下是视频,则不需要多部分。我建议使用二进制编码而不是任何其他字符编码来提高速度并保持二进制数据的完整性。
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];
祝你好运!
答案 1 :(得分:0)
也许你应该看看响应,也许 - 错误:
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);
}
答案 2 :(得分:0)
因为看起来你假装是一个表格,我建议使用ASIFormDataRequest
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:videoData withFileName:@"videofile.3gp" andContentType:@"video/3gpp" forKey:@"video"];