我想以png格式将图像上传到服务器。我必须为请求提供三个参数
transactionID: (any random integer)
signatureBytes:data:image/png;base64,iVBORw0KGgoAAAsdffsdfsdfsdfsdfYAAAAeGRPoAAAgAElEQVR4nO2de5BmRXnGHzWmYkKVKUqJRk0erwerrwds55/lVnT/mm+87/Z4+3f30+/YN6D9L48CMA9VxnOJ9S
guid: CAD1CBE7-B5A4-43d6-BCDD-89B16F97E3C4
我使用过代码:
NSData *imageData = UIImageJPEGRepresentation(image.image,90);
// setting up the URL to post to
NSString *urlString=@"http://www.riverport.net/icalibur/dev/signature.ashx";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"transactionID\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[[NSString stringWithString:@"7" intva] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"signatureBytes\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"data:image/png;base64,iVBUaBCrFDXGSIECEXEFdUFEhEVAluXmsrrtrrttrrqddcsvrgergergg/YN6D9L4uozc5h+nsWsFhGSn88CWAAwqV2ynoz1qvJjYTmfCOk1QxJ0ANgHijoZDy8CMA9VxnOJ9S"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"guid\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"CHJ78G-B5A4-43d6-BCDD-CFG567FGJ"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSString* returns= [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
//NSLog(@"body %@",stri);
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
来自服务器的响应是:
[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +201
Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +66
[InvalidCastException: Conversion from string "����
有任何人知道可能是什么问题。我不知道我做得对。
答案 0 :(得分:3)
根据您收到的错误,我相信您没有为请求正确设置参数。
为什么不使用ASIHTTPRequest上传照片?它有一个ASIFormDataRequest类,你可以使用它来发出POST或GET请求,你可以很容易地添加参数。
here是使用ASIHTTPRequest上传图片的示例。
答案 1 :(得分:2)
您没有正确格式化上传数据。帖子数据应如下所示:
--boundary
Content-Disposition: form-data; name="foo"
value-for-foo
--boundary
Content-Disposition: form-data; name="bar"
value-for-bar
--boundary
Content-Disposition: form-data; name="file"; filename="filename.ext"
Content-Type: application/whatever
file data
--boundary--
注意标题和字段值之间是否有空行(“\ r \ n \ r \ n”),值后面是一个crlf,下一个字段的标题之前是否重复边界。你的代码中没有这个,你的输出看起来更像是这样:
--boundary
Content-Disposition: form-data; name="foo"
value-for-fooContent-Disposition: form-data; name="bar"
value-for-barContent-Disposition: form-data; name="file"; filename="filename.ext"
Content-Type: application/whatever
file data--boundary--
答案 2 :(得分:0)
嗨Pankaj 我不确定,但只是尝试使用outID和guide参数的图像NSData, 你的代码似乎没问题。 请让我知道结果。
答案 3 :(得分:0)
您说要发布PNG图像,但是您要在代码段中获取JPEG表示。是否通过更改为 UIImagePNGRepresentation 来解决?