使用 C# Web 服务将图像从 android 应用程序上传到服务器

使用 C# Web 服务将图像从 android 应用程序上传到服务器

问题描述:

我想使用 c# web 服务将图像上传到 IIS 服务器.我为此编写了 Web 方法如下:

I want to upload image to IIS server using c# web service. I have written the web method for this as follows:

[WebMethod] 
public string UploadFile(byte[] f, string fileName)
{
    try
    {
       MemoryStream ms = new MemoryStream(f);

       FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath
       ("~/TransientStorage/") +fileName, FileMode.Create); 

       ms.WriteTo(fs); 

       ms.Close();
       fs.Close();
       fs.Dispose(); 

       return "OK";
    }
    catch (Exception ex)
    {
        // return the error message if the operation fails
        return ex.Message.ToString();
    }
}

这里的 web 方法将参数作为 byte[] .我已将 sdcard 图像转换为 byte[] 但是当我将它作为 URL 参数传递时它不起作用.我也尝试通过转换 byte[]数组到 base64 字符串仍然不起作用.

Here the web method is taking argument as byte[] .I have convert the sdcard image to byte[] but when i am passing it as an URL paramete it is not working .I have also tried by converting the byte[] array to base64 strig still it is not working.

谁能告诉我如何使用c#网络服务将图片上传到IIS服务器.

Can any one tell me how to upload image to IIS server using c# web service.

您需要将它POST 到服务器,设置内容类型并包括字节数组作为帖子的正文.

You'll need to POST it to the server, setting the content type and including the byte array as the body of the post.

参见 hereC#此处 为 Android 示例.

See here for an example of doing it from C# and here for an example for Android.