使用iPhone sdk上传png格式的图片

发布于 2024-10-30 13:38:08 字数 3500 浏览 3 评论 0原文

我想将 png 格式的图像上传到服务器。我必须在请求中提供三个参数,即

  • transactionID: (任意随机整数)

  • signatureBytes:data:image/png;base64,iVBORw0KGgoAAAsdffsdfsdfsdfsdfYAAAAeGRPoAAAgAElEQVR4nO2de5BmRXnGHzWmYkKVKUqJRk0erwerrwds55/lVnT/mm+87/Z4+3f30+/YN6D9L 48CMA9VxnOJ9S

  • <代码>引导: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 &quot;����

有人知道可能是什么问题。我不知道我是否在做没错。

I want to upload an image in png format to a server.I have to give three parameters with the request which are

  • transactionID: (any random integer)

  • signatureBytes:data:image/png;base64,iVBORw0KGgoAAAsdffsdfsdfsdfsdfYAAAAeGRPoAAAgAElEQVR4nO2de5BmRXnGHzWmYkKVKUqJRk0erwerrwds55/lVnT/mm+87/Z4+3f30+/YN6D9L48CMA9VxnOJ9S

  • guid: CAD1CBE7-B5A4-43d6-BCDD-89B16F97E3C4

I have used the code:

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);

and the response from server is:

[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 "����

Have anybody any idea What could be the problem.I do not know if i am doing it right .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天荒地未老 2024-11-06 13:38:09

您没有正确格式化上传数据。发布数据应如下所示:

--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--

You're not formatting the upload data correctly. The post data should look something like this:

--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--

Note how there is a blank line ("\r\n\r\n") in between the headers and the value of the field, a crlf after the value, and a repeat of the boundary before the next field's header. You have none of that in your code, your output looks more like this:

--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--
陌上芳菲 2024-11-06 13:38:09

嗨潘卡杰
我不确定,但只尝试不带 transactionID 和引导参数的图像 NSData,
你的代码看起来没问题。
请让我知道结果。

Hi Pankaj
I am not sure but just try only image NSData with out transactionID and guide parameter,
your code seems ok.
Please let me know result.

嘴硬脾气大 2024-11-06 13:38:09

您说您想发布 PNG 图像,但您正在代码片段中获取 JPEG 表示形式。是不是改成UIImagePNGRepresentation就解决了?

You say you want to post a PNG-image but you are fetching the JPEG-representation in your code-snippet. Is it solved by changing to UIImagePNGRepresentation?

我很坚强 2024-11-06 13:38:08

从您收到的错误来看,我相信您没有为请求正确设置参数。

为什么不使用 ASIHTTPRequest 上传照片?它有一个 ASIFormDataRequest 类,您可以使用它来发出 POST 或 GET 请求,并且可以非常轻松地向其添加参数。

此处是使用 ASIHTTPRequest 上传图像的示例。

From the errors you received i believe that you don't set your parameters correctly for the request.

Why don't you use ASIHTTPRequest to upload your photo? it has a class ASIFormDataRequest that you can use to make POST or GET requests and you can add parameters to it really easy.

here is an example of uploading images with ASIHTTPRequest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文