将帖子数据添加到图像上传

发布于 2024-11-09 18:33:44 字数 2099 浏览 0 评论 0原文

我使用 这个答案这个问题。本质上,代码创建了格式正确的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

○愚か者の日 2024-11-16 18:33:44

这并不太难。多部分/表单数据帖子的一般格式如下:

--boundary-string
Content-Disposition: form-data; name="field1"

value1
--boundary-string
Content-Disposition: form-data; name="field2"

value2
--boundary-string--

文件上传是一种特殊情况,但您已经处理了该情况。要添加另一个字段,只需添加另一个块即可。像这样的事情应该可以做到:

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Comment\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[comment dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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];

顺便说一句,你几乎不需要调用 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:

--boundary-string
Content-Disposition: form-data; name="field1"

value1
--boundary-string
Content-Disposition: form-data; name="field2"

value2
--boundary-string--

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:

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Comment\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[comment dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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];

BTW, you pretty much never need to call NSString's stringWithString: method. If you need a copy, call copy, and otherwise just use the string you already have. NSMutableString's stringWithString: can be useful as an alternative to [[string mutableCopy] autorelease] though.

不奢求什么 2024-11-16 18:33:44

首先,您应该使用 NSMutableURLRequest 对象的 addValue:forHTTPHeaderField: 方法来添加标头。例如,

[request addValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"form-data; name=\"userfile\"; filename=\"%@\"",filename] forHTTPHeaderField:@"Content-Disposition"];
...

对于代码中的第二个,在按照上面所述设置所有标头之后,您应该使用下面的代码设置 POST 数据。

NSMutableData *body = [[NSMutableData alloc] init];
[body appendData:[[NSString stringWithString:@"name=myimagename.png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"&data="] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body release];
[request setHTTPBody:body];

但当且仅当服务器支持这种格式时它才会起作用(我的意思是data=image binary data&name=image name)。

First of all you should use addValue:forHTTPHeaderField: method of NSMutableURLRequest object to add headers. For example

[request addValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"form-data; name=\"userfile\"; filename=\"%@\"",filename] forHTTPHeaderField:@"Content-Disposition"];
...

For second in your code after all headers are set as I described above you should set POST data with code below.

NSMutableData *body = [[NSMutableData alloc] init];
[body appendData:[[NSString stringWithString:@"name=myimagename.png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"&data="] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body release];
[request setHTTPBody:body];

But it will work if and only if server supports such format (I mean data=image binary data&name=image name).

霓裳挽歌倾城醉 2024-11-16 18:33:44

我建议使用 ASIHttp。请参阅 ASIFormDataRequest

I would suggest to use ASIHttp. See ASIFormDataRequest

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文