我想使用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]]);
}
}];
}
答案 0 :(得分:1)
您的请求似乎是正确的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]];
}