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

标签 ios objective-c nsurlconnection

我想使用 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/

相关文章:

iphone - iOS同步下载过程中的超时间隔?

ios - Swift - 发送 POST 请求时从 NSURLSession 返回数据

ios - 由于未捕获的异常 'NSUnknownKeyException',不断出现错误/崩溃

android - Ionic 有没有办法直接在 Android 或 iOS 设备上进行调试?

iphone - 将我的 UIView 添加到 Storyboard 中的 ViewController

ios - 响应手指拖动更改 UIView 的高度

iphone - NSURLConnection 的 initWithRequest 什么时候返回 nil

ios - 如何快速获取正确的字符串数组?

ios - Nativescript 插件在 Podfile 中指定 Pod 版本

ios - 如何创建一个 UIButton 来允许我执行 2 个 IBAction iOS - Objective-C