上传多部分/Formdata图像,获取文件 [英] Multipart/Formdata image upload, get the file

查看:62
本文介绍了上传多部分/Formdata图像,获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想将通过UIImagePickerController选择的图像上传到仅接受JPEG图像的数据库.我在这里和其他论坛中浏览了许多问题,但仍然无法正常工作.我希望您可以检查我的代码,如果有我看不到的错误. 这是上传完整的方法,带有图像说明,图像标题和该图像的地理数据:

in my app i want to upload an image chosen via UIImagePickerController to a database which only accepts JPEG images. I've browsed many questions in here, and in other forums but i still didn't get it to work. I hope you can check my code, if there is a mistake which i can't see. This is the complete method for uploading, with an image description, an image title, and the geodata for this image:

- (void) uploadPic{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *destinationFilePath = [[NSString alloc] initWithFormat: @"%@/%@.jpeg", documentsDirectory, self.chosenImage.imgTitle];
    NSURL *fileURL = [[NSURL alloc]initWithString:destinationFilePath];
    NSLog(@"will upload file: %@", destinationFilePath);

    NSData *imageURLdata = [[NSData alloc]initWithContentsOfURL:fileURL];   (*1) 
    NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 90);    (*2) 
    //Here i tried 2 ways of getting the data for uploading, but both don't work.

    // create the URL
    NSURL *postURL = [NSURL URLWithString:@"http://*********/PictureUpload"];

    // create the connection
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];

    // change type to POST (default is GET)
    [postRequest setHTTPMethod:@"POST"];

    // just some random text that will never occur in the body
    NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";

    // header value, user session ID added
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
                                sessionID];

    // set header
    [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
    // create data
    NSMutableData *postBody = [NSMutableData data];

    // title part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[self.chosenImage.imgTitle dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // desc part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"desc\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[self.chosenImage.imgDescription dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


    // latitude part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[self.chosenImage.latitude dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // longitude part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[self.chosenImage.longitude dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // media part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.jpeg\"\r\n", self.chosenImage.imgTitle ] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[NSData dataWithData:imageURLdata]];
    [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    // final boundary
    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *s = [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding];
    NSLog(@"%@", s);

    // add body to post
    [postRequest setHTTPBody:postBody];

    // pointers to some necessary objects
    NSURLResponse* response;
    NSError* error;

    // synchronous filling of data from HTTP POST response
    NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];

    if (error)
    {
        NSLog(@"Error: %@", [error localizedDescription]);
    }

    // convert data into string
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes]
                                                        length:[responseData length]
                                                      encoding:NSUTF8StringEncoding];

    // see if we get a welcome result
    NSLog(@"%@", responseString);
    [self responseHandler:responseString];

}

ImagePickerController中选择了UIImage之后,通过CGImageRef创建GEOimage chosenImage. 方法号. * 1以获得NSData上载不是最好的,因为这里是从文档目录中选择图像,并且这里删除了有关该图像的所有EXIF信息. 通过这两种方法,我都从数据库中得到了不支持图像文件类型的响应(需要JPEG). 我想用方法nr. * 2我正在发送JPEG图像,但是也许我在整个multipart/formdata过程中都犯了一个错误.

The GEOimage chosenImage is created via the CGImageRef after an UIImage is chosen in the ImagePickerController. Method nr. *1 to get the NSData for upload is not the best, because here the image is chosen from the document directory, and here every EXIF information about the image is deleted. With both methods i get the response from the database that the image filetype is not supported (JPEG expected). I thought with method nr. *2 i'm sending an JPEG image, but perhaps i have a mistake in the whole multipart/formdata process.

我试图获取文件系统上原始文件的URL,但这对我来说很难.我只从图片中获得了资源网址.

I tried to get the URL to the original file on the filesystem, but this is quite difficult for me. I only got the assets-url from the image.

预先感谢

S4lfish

推荐答案

您现在可能已经自行解决了,但是我想我看到了您的问题.

You've probably solved it on your own by now, but I think I see your problem.

NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";

// header value, user session ID added
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
                            sessionID];

当您在MIME标头中定义边界时,您将使用sessionID作为边界.然后,在下面,使用stringBoundary变量创建实际边界.

When you're defining the boundary inside the MIME header, you're using your sessionID as the boundary. Then, down below, you're using your stringBoundary variable to create the actual boundaries.

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

提供每个MIME块的Content-Length来帮助处理代码也是一个好主意,但我认为这不是问题所在.

It might also be a good idea to provide the Content-Length of each MIME block to help the processing code, but I don't think that's at issue here.

这篇关于上传多部分/Formdata图像,获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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