2015-05-29 84 views
0

我想上傳圖片使用http post但我的代碼導致500錯誤。如何用httpPost上傳幾張圖片?

一個文件可以,但兩個文件或更多的文件會導致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

你所得到的500錯誤是一個內部服務器錯誤。您必須與服務器開發人員交談,因爲這是服務器錯誤。 – rckoenes

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

    }