我正在将图片(jpg)上传到.net服务。如果我上传小图片,上传效果很好,但是当我发送“大”图像(超过1 Mb或“大”尺寸)图像上传成功时,图像看起来像这样
我通过curl测试了网络服务,它运行正常。
任何想法? 谢谢你!!!!!!
我的上传代码是:
-(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 :(得分:0)
最后我改变了我的代码,我不需要内容长度(我推测服务器推断它)
新代码(工作正常):
- (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];
}