目标c - 通过http POST发送图像 [英] Objective c - Send an image over http POST

查看:171
本文介绍了目标c - 通过http POST发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解如何使用http POST和我当前的客户端 - 服务器协议设计发送图像。
从客户端到服务器的所有消息类似于下面的示例,有一个带参数 cmd 的cmd字符串以及该命令的一些更相关的参数。

I'm trying to understand how to send an image with http POST and my current client-server protocol design. All the messages from client to server looks like the example below, there is a cmd string with parameter cmd and some more relevant parameters for the command.

例如,这就是我向服务器发送短信的方式:

For example this is how i send a text message to the server:

- (void)sendMessagesWithText:(NSString *)text fromUser:(NSString *)userId
{
    NSString *url = SERVER_URL;

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"cmd=%@&userid=%@&msgtext=%@", 
                       @"sendmessage", 
                       userId,
                       text] dataUsingEncoding:NSUTF8StringEncoding]];


    [request setHTTPBody:body];

    // send to server
    [[NetworkHelper sharedManager] sendRequest:request]; 
}

现在我想让用户也发送图像,但是怎么做我用我的协议设计发送它?我应该在cmd字符串后将图像附加到正文吗?

Now I want to enable the user to send also an image, but how do I send it with my protocol design? should i just append the image to the body after the cmd string?

推荐答案

您需要发布multipart / form-data。

You need to post with multipart/form-data.

以下是现已解散的ASI的示例:创建和运行请求

Here is the example from the now defunct ASI: Creating and running requests


使用ASIFormDataRequest发送表单POST

Sending a form POST with ASIFormDataRequest

以兼容的方式发送POST数据网页表单,使用包含的ASIFormDataRequest子类。在上传二进制数据或文件时,数据以application / x-www-form-urlencoded格式或multipart / form-data格式发布。根据需要从磁盘读取文件中的数据,因此只要您的Web服务器设置为处理它们,就可以POST POST大文件。

To send POST data in a manner compatible with web page forms, use the included ASIFormDataRequest subclass. Data is posted in ‘application/x-www-form-urlencoded’ format, or ‘multipart/form-data’ format when uploading binary data or files. Data in files is read as needed from disk, so POSTing large files is OK, as long as your web server is setup to handle them.



ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];




ASIFormDataRequest将自动检测mime类型的文件(在iOS 3.0或更高版本上)通过setFile:forKey:添加到POST的Mac),并将其包含在发送到服务器的mime标头中。如果您愿意,可以使用较长的表格覆盖:

ASIFormDataRequest will autodetect the mime type of files (on iOS 3.0 or later an Mac) added to the POST via setFile:forKey:, and include this in the mime headers sent to the server. If you prefer, you can use the longer form to override this:






Multipart / form-data 很糟糕。相信我,几年前我用C ++实现了它。让其他人去做肮脏的工作。


Multipart/form-data sucks. Trust me, I've implemented it in C++ a number of years ago. Get someone else to do the dirty work.

但如果你还想独自去做。我找到了这个这个。祝你好运。

But if you still want to go it alone. I found this and this. Good Luck.

这篇关于目标c - 通过http POST发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆