ios 使用 HTTP POST 上传图像和文本

标签 ios file-upload curl http-post image-uploading

感谢阅读。

我是 iOS 新手,我正在尝试使用 multi-part form encoding 上传图片和文本在 iOS 中。

curl等效是这样的:curl -F "param1=value1" -F "param2=@testimage.jpg" "<a href="http://some.ip.address:5000/upload" rel="noreferrer noopener nofollow">http://some.ip.address:5000/upload</a>"

curl上面的命令在 JSON. 中返回预期的正确响应

问题: 我不断收到 HTTP 400 请求,这意味着我在编写 HTTP POST 正文时做错了。

我做了什么: 作为引用,我尝试了 Flickr API iOS app "POST size too large!"Objective C: How to upload image and text using HTTP POST? .但是,我不断收到 HTTP 400。

我尝试了 ASIHttpRequest但那里有一个不同的问题(回调从未被调用)。但是,我没有对此进行进一步调查,因为我听说开发人员已停止支持该库:http://allseeing-i.com/[request_release] ;

有人可以帮帮我吗?

最佳答案

这是我的应用程序的代码,用于将图像发布到我们的网络服务器:

// Dictionary that holds post parameters. You can set your post parameters that your server accepts or programmed to accept.
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:[NSString stringWithString:@"1.0"] forKey:[NSString stringWithString:@"ver"]];
[_params setObject:[NSString stringWithString:@"en"] forKey:[NSString stringWithString:@"lan"]];
[_params setObject:[NSString stringWithFormat:@"%d", userId] forKey:[NSString stringWithString:@"userId"]];
[_params setObject:[NSString stringWithFormat:@"%@",title] forKey:[NSString stringWithString:@"title"]];

// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = [NSString stringWithString:@"----------V2ymHFg03ehbqgZCaKO6jy"];

// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ 
NSString* FileParamConstant = [NSString stringWithString:@"file"];

// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:@""]; 

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];                                    
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];

// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

// post body
NSMutableData *body = [NSMutableData data];

// add params (all params are strings)
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}

// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imageData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];

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

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long) [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

// set URL
[request setURL:requestURL];

关于ios 使用 HTTP POST 上传图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8564833/

相关文章:

ios - 在 TableView 中呈现模态视图 Controller

amazon-web-services - Spring Boot 多部分文件上传大小不一致

Java Spring MultipartFile 上传多个文件时 Controller 仅看到第一个文件

node.js - URL 在浏览器中加载,但不在终端中加载(curl 或 Node.js)

ios - IOS中获取图像坐标

ios - "Unexpectedly found nil"但已使用 "print"成功检查该值。 swift

ios - 为什么 UIBezierPath 比 CAShapeLayer 快

php - 使用Php将excel文件上传到数据库

java - Spring -oauth2 : HTTP Status 403 - Access Denied

c - gcc 不会在 OS X 的命令行上包含 libcurl