0

I'm uploading images (jpg) to .net service. If I upload small images the upload works great, but when I send "big" images (more than 1 Mb or "big" size) images upload success, but the image looks like this

Image uploaded

I tested web service via curl and it works ok.

Any Idea? Thanks you!!!!!!

My upload code is:

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

}

pcambre
  • 141
  • 11
  • Have you tried adding the file size to the Content-Disposition header? (i.e. size:). I believe you could also add a Content-Length header as well. – Steve Mallory Jan 08 '13 at 13:47
  • Content-length is already set as header.I try moving content-disposition to header but error continues. – pcambre Jan 08 '13 at 14:33

1 Answers1

0

Finally I change my code, I don't need content-length (I sopouse server infers it)

New code (work 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];

}

pcambre
  • 141
  • 11