将帖子数据添加到图像上传
我使用 这个答案这个问题。本质上,代码创建了格式正确的 html 流并提交它。
为了完整性,我重新发布了代码。
- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{
NSString *urlString = @"http://www.yourdomainName.com/yourPHPPage.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
return ([returnString isEqualToString:@"OK"]);
}
但是,我不够聪明:( 无法弄清楚如何添加通用字段值,例如。我想添加一个要与图像文件一起提交的注释字段。
有关如何修改该代码以添加的任何建议POST 表单的多个字段
谢谢
更新:我可以通过修改我发布的 URL 以包含 GET 参数来使其工作,但我宁愿使用所有发布参数。
I am posting an image from my IPhone app to a php page on a website using the code in this answer to this question. Essentially the code creates the correctly formatted html stream and submits it.
For Completeness, I have reposted the code.
- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{
NSString *urlString = @"http://www.yourdomainName.com/yourPHPPage.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
return ([returnString isEqualToString:@"OK"]);
}
However I'm not clever enough :( to figure out how to add a general purpose field value eg. I would like to add a Comment field to be submitted along with the image file.
Any suggestions on how I could modify that code to add a number of fields to the POST form.
Thank you.
Update: I can get it to work by modifying the URL I post to to include GET Parameters, but I would rather use all post params.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不太难。多部分/表单数据帖子的一般格式如下:
文件上传是一种特殊情况,但您已经处理了该情况。要添加另一个字段,只需添加另一个块即可。像这样的事情应该可以做到:
顺便说一句,你几乎不需要调用 NSString 的
stringWithString:
方法。如果您需要副本,请调用copy
,否则只需使用您已有的字符串。 NSMutableString 的stringWithString:
可以作为[[string mutableCopy] autorelease]
的替代品。It's not too difficult. The general format of a multipart/form-data post is like this:
File uploads are a special case, but you already have that handled. To add another field, you just add another block. Something like this should do it:
BTW, you pretty much never need to call NSString's
stringWithString:
method. If you need a copy, callcopy
, and otherwise just use the string you already have. NSMutableString'sstringWithString:
can be useful as an alternative to[[string mutableCopy] autorelease]
though.首先,您应该使用 NSMutableURLRequest 对象的
addValue:forHTTPHeaderField:
方法来添加标头。例如,对于代码中的第二个,在按照上面所述设置所有标头之后,您应该使用下面的代码设置 POST 数据。
但当且仅当服务器支持这种格式时它才会起作用(我的意思是data=image binary data&name=image name)。
First of all you should use
addValue:forHTTPHeaderField:
method of NSMutableURLRequest object to add headers. For exampleFor second in your code after all headers are set as I described above you should set POST data with code below.
But it will work if and only if server supports such format (I mean data=image binary data&name=image name).
我建议使用 ASIHttp。请参阅 ASIFormDataRequest
I would suggest to use ASIHttp. See ASIFormDataRequest