2013-01-08 145 views
0

我正在將映像(jpg)上傳到.net服務。如果我上傳小圖片上傳的偉大工程,但是當我送「大」圖像(超過1 MB或「大」尺寸)圖像上傳成功,但圖像看起來像這樣將IOS映像上傳到.net服務

Image uploaded

我測試通過捲曲的Web服務,它工作正常。

任何想法? 謝謝你!!!!!!

我上傳的代碼是:

-(void)uploadJPEGImage:(NSString*)requestURL image:(UIImage*)image pictureName: (NSString*)pictureName { 
NSURL *url = [[NSURL alloc] initWithString:requestURL];  
NSMutableURLRequest *urequest = [NSMutableURLRequest requestWithURL:url]; 

[urequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[urequest setHTTPShouldHandleCookies:NO]; 
[urequest setTimeoutInterval:600]; 

NSData *postImageData = UIImageJPEGRepresentation(image, 0.9); 


NSMutableData *body = [NSMutableData data]; 
[urequest setHTTPMethod:@"POST"]; 
//STEP2 Creating Boundary 
// There are no rules as of the content of the boundary but as it must not occur in any of the parts of your message content is usually a randomly generated sequence of numbers, letters or combination of both in order to guarantee uniqueness and differentiate from any possible dictionary words. 
NSString *boundary = @"---------------------------14737809831466499882746641449"; 

//Setting POST Header 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[urequest addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

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

//STEP4 Adding additional parameters 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",pictureName] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
//STEP5 Appending Image Data 
[body appendData:[NSData dataWithData:postImageData]]; 


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

[urequest setHTTPBody:body]; 

//Set content length 

NSString* postlength = [NSString stringWithFormat:@"%d", [body length]]; 
[urequest setValue:postlength forHTTPHeaderField:@"Content-Length"]; 


[url release]; 
NSURLConnection *conaux =[[NSURLConnection alloc] initWithRequest:urequest delegate:self startImmediately:NO]; 
self.connection = conaux; 
[conaux release]; 
NSMutableData *auxdata = [[NSMutableData alloc] init]; 
self.receivedData = auxdata; 
[auxdata release];  

}

+0

您是否嘗試將文件大小添加到Content-Disposition標題? (即大小:以字節爲單位的大小)。我相信你也可以添加一個Content-Length頭。 –

+0

內容長度已被設置爲標題。我嘗試將內容處置移動到標題,但錯誤仍在繼續。 – pcambre

回答

0

最後我改變我的代碼,我並不需要內容長度(我sopouse服務器推斷出它)

新的代碼(工作OK):

- (void)uploadJPEGImage:(NSString*)requestURL image:(UIImage*)image pictureName:(NSString*)pictureName { 
NSURL *url = [[NSURL alloc] initWithString:requestURL]; 
NSMutableURLRequest *urequest = [NSMutableURLRequest requestWithURL:url]; 

[urequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[urequest setHTTPShouldHandleCookies:NO]; 
[urequest setTimeoutInterval:60]; 
[urequest setHTTPMethod:@"POST"]; 

NSString *boundary = @"---------------------------14737809831466499882746641449"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[urequest setValue:contentType forHTTPHeaderField: @"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 
// add image data 
NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 
if (imageData) { 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", pictureName] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:imageData]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[urequest setHTTPBody:body]; 
[url release]; 
NSURLConnection *conaux =[[NSURLConnection alloc] initWithRequest:urequest delegate:self startImmediately:NO]; 
self.connection = conaux; 
[conaux release]; 
NSMutableData *auxdata = [[NSMutableData alloc] init]; 
self.receivedData = auxdata; 
[auxdata release]; 

}