gpt4 book ai didi

ios - Multipart/Formdata 图片上传,获取文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:21 24 4
gpt4 key购买 nike

在我的应用程序中,我想将通过 UIImagePickerController 选择的图像上传到只接受 JPEG 图像的数据库。我在这里和其他论坛浏览了很多问题,但我仍然没有让它工作。我希望你能检查我的代码,如果有我看不到的错误。这是完整的上传方法,包括图像描述、图像标题和该图像的地理数据:

- (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];

}

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

我试图获取文件系统上原始文件的 URL,但这对我来说非常困难。我只从图片中得到了 assets-url。

提前致谢

S4lfish

最佳答案

你现在可能已经自己解决了,但我想我明白了你的问题。

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

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

当您在 MIME header 内定义边界时,您使用的是 sessionID 作为边界。然后,在下方,您将使用 stringBoundary 变量来创建实际边界。

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

提供每个 MIME block 的 Content-Length 以帮助处理代码也可能是个好主意,但我认为这不是问题所在。

关于ios - Multipart/Formdata 图片上传,获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8561403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com