发送图像到.NET Web服务 [英] Send image to .NET web service

查看:269
本文介绍了发送图像到.NET Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发使用.NET Web服务iOS应用程序。在的网络服务之一,我要送形象的 HttpPostedFileBase 在一个名为参数的文件的,这将返回图像的URL和状态参数的响应。其实我不知道什么是HttpPostedFileBase。但我想发送的字节数。我得到的图像的URL从WS为空。我猜WS调用工作正常,但问题是我发送图像的方式。我现在用的是下面的code图像数据追加到HTTP主体。

I am developing an iOS application using .NET web service. In one of the web services, I have to send image as HttpPostedFileBase in a parameter called "file", which will return the image URL and status parameters as response. Actually I am not sure what is HttpPostedFileBase. But I am trying to send that as bytes. I am getting image URL as null from WS. I guess the WS call works fine, but the problem is in the way I send the image. I am using the following code to append image data to HTTP body.

NSData *imageData = UIImageJPEGRepresentation(image, 1);
[postData appendData:[[NSString stringWithFormat:@"--%@\r--\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"file.jpeg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[request addValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%i", [postData length]] forHTTPHeaderField:@"Content-Length"];
[postData appendData:[[NSString stringWithFormat:@"--%@\r--\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

请让我知道我怎么能得到这个工作。让我知道你是否需要再详细信息。

Please let me know how can I get this working. Let me know if you need anymore detail.

感谢。

推荐答案

对不起球员。问题是在HTTP标头。 (使用HTTP POST IOS上传图片和文字)链接发表@FahriAzimov上述帮助了我。我的坏,其实我很困惑,有将是对.NET Web服务不同的格式。

Sorry guys. The problem was in the HTTP headers. The link (ios Upload Image and Text using HTTP POST) posted by @FahriAzimov above helped me. My bad, actually I was confused that there is going to be a different format for .NET web services.

我只好送形象,因为我们做了正常的网络服务。工作code是如下:

I had to send the image as we do for normal web services. The working code is below:

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

非常感谢大家的帮助。

Thanks a lot for everyone's help.

这篇关于发送图像到.NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆