在URL POST请求中发送其他参数以及图像数据

时间:2014-02-11 23:23:36

标签: php ios image-uploading

我正在尝试将Image与其他数据一起发送到PHP。

PHP代码:

if (isset($_FILES['image'])) {        
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    .....INSERT INTO DB.....WORKS FINE....
}

有人可以告诉我如何通过这个图像发布以下iOS代码中的其他数据,如“id”,“index”等,以便我可以通过$ _POST [“id”]和$ _POST [“访问它来自php页面的索引“]。

iOS代码:

NSURL *baseURL = [NSURL URLWithString:BASE_URL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[baseURL URLByAppendingPathComponent:@"upload_image.php"]];
[request setHTTPMethod:@"POST"];

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

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

NSData *imageData = UIImageJPEGRepresentation(image, 90);
[body appendData:[NSData dataWithData:imageData]];

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

[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];


[NSURLConnection
 sendAsynchronousRequest: request
 queue: [NSOperationQueue new]
 completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

     NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
     int code = [httpResponse statusCode];

     NSLog(@"%d == %@", code, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

 }];

2 个答案:

答案 0 :(得分:1)

NSMutableData *body = [NSMutableData data];

只需添加:

// add params (all params are strings), _params is a Dictionary
for (NSString *param in _params) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] 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]];
}

然后添加图片

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
...

答案 1 :(得分:0)

选中此项以使用POST方法将图像和参数(字符串)上传到服务器

 // 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=%@", boundary];
 [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", boundary] 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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

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

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

 // set URL
 [request setURL:requestURL];