我正在将图像 (jpg) 上传到 .net 服务。如果我上传小图像,上传效果很好,但是当我发送“大”图像(超过 1 Mb 或“大”大小)图像上传成功,但图像看起来像这样
我通过 curl 测试了 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];
}