【问题标题】:Upload image , video and audio file to a php web service in iphone将图像、视频和音频文件上传到 iphone 中的 php web 服务
【发布时间】:2011-12-25 11:35:11
【问题描述】:

我在将媒体文件上传到内置于 php 的 Web 服务时遇到问题。 我有一个应用程序以及 android 中的源代码,我想在 iphone 中复制它。 在这部分,我应该将媒体文件和一些字符串上传到 Web 服务。 android 中的代码使用一个名为 MultiPartEntity 的类和 HTTPPost 来上传它。 有几个标签,标签与一些字符串相关联。

for ex.MultiPartEntity mpEntity.addPart("username", "abc");,同样适用于密码和其他标签。

我做了一些研究,发现 ASIFormDataRequest 可以处理类似的事情。

这是我用来上传媒体的示例代码以及其他一些细节,

NSData *imageData = UIImageJPEGRepresentation(image.image, 90); NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [请求 setURL:[NSURL URLWithString:@"http://my.url.com/somephpwebservice.php"]]; [请求setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

// now lets create the body of the post 
NSMutableData *body = [NSMutableData data];

//................................ Append the image data here//u= \"TP\";p= \"7012\";o= \"DEMO\";j= \"1151\"...........................
//if (!([htmlCodeToWrite rangeOfString:@"SignatureShouldBeDoneHere"].location == NSNotFound))
//{
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data;name=\"userfile\";username=\"TP\";p=\"7012\";filename=\"%@\"\r\n",@"superman.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
    //[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
//}
//NSString *content = [NSString stringWithUTF8String:[imageData bytes]];
NSLog(@"%@",imageData);
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//data into the string..... verifying............
NSString *strTest = [[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding];
NSLog(@"%@ %@",@".....the request for webService",strTest);

// setting the body of the post to the request
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

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

NSLog(@"%@ %@",returnString,@".....the response from webService");

这是正确的程序吗??

php 开发者说图片没有上传。

请帮忙。 提前谢谢。

【问题讨论】:

  • 为什么不使用 ASIFormDataRequest 的任何特殊原因?
  • 我什至用 ASIFormDataRequest 尝试过,但没有帮助

标签: php iphone web-services


【解决方案1】:

请参阅我在this 帖子中的回复。我认为这是符合分段上传文件请求的正确方法。干杯!

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 2014-04-26
    • 1970-01-01
    • 2018-02-19
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多