gpt4 book ai didi

ios - 如何使用 httpPost 上传多张图片?

转载 作者:行者123 更新时间:2023-11-29 01:59:20 25 4
gpt4 key购买 nike

我想使用 http post 上传图片,但我的代码导致了 500 错误。

一个文件可以,但是两个文件或更多文件会导致500错误。

是不是服务器问题?我不知道为什么。

这是我的上传代码部分:

-(void)sendImageByPost:(NSMutableArray *)images{
//create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

//Create boundary, it can be anything
NSString *boundary = @"------VohpleBoundary4QuqLuM1cE5lMwCy";

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

NSString *FileParamConstant = @"imageParamName";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *now;
NSString *date;
[formatter setDateFormat:@"yyyyMMddhhmmss"];


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

for(UIImage * image in images){
NSData *imageData = UIImageJPEGRepresentation(image, 1);
[NSThread sleepForTimeInterval:1];
now = [NSDate date];
date = [formatter stringFromDate:now];

//Assuming data is not nil we add this to the multipart form
if (imageData)
{
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", FileParamConstant, date] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
}
}


[body appendData:[@"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

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

// set URL
[request setURL:[NSURL URLWithString:@"http://52.68.115.148/api/fileupload"]];

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

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

if ([httpResponse statusCode] == 201) {

NSLog(@"success");
}
else{
NSLog(@"fail");
NSLog(@"%@", [NSString stringWithFormat:@"%d", [httpResponse statusCode]]);
}

}];
}

最佳答案

您的请求正文似乎不正确 multipart/form-data。

您在每张图片后都忘记了边界。尝试改变

if (imageData)
{
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", FileParamConstant, date] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
}

if (imageData)
{
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.jpg\"\r\n", FileParamConstant, date] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

}

关于ios - 如何使用 httpPost 上传多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524478/

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