Skip to main content
 首页 » 编程设计

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

2024年02月24日43mate10pro

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

[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 strig 仍然无法正常工作。

谁能告诉我如何使用 C# Web 服务将图像上传到 IIS 服务器。

请您参考如下方法:

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

参见 here以 C# 和 here 为例以 Android 为例。