我想使用Windows应用程序将文件上传到Web服务,以便Web服务可以处理该文件。
请告诉我如何实现这一目标。
我只知道我可以使用带有Windows窗体的Web服务来只发送字符串,int,这些类型。但是文件呢。
感谢任何帮助
答案 0 :(得分:2)
如果使用WebService,我们通常会定义某种web方法 它接受一个字节数组参数和一个字符串参数,如
public void UploadFile(bytes as Byte(), filename as String)
然后,我们可以轻松地在.NET应用程序中调用它,因为我们可以使用 WSDL.EXE或VS.NET生成一个easytouse客户端代理类。
答案 1 :(得分:1)
正如Will Will所说,你总是可以声明一个web方法,它在你的web服务中输入一个byte []作为输入,但是如果你不喜欢在你的web服务调用中发送字节数组,你可以始终将byte []编码为客户端的base64字符串,并解码服务器端的byte []
实施例
WebService示例Web方法
[WebMethod]
public bool UploadFile(string fileName, string uploadFileAsBase64String)
{
try
{
byte[] fileContent = Convert.FromBase64String(uploadFileAsBase64String);
string filePath = "UploadedFiles\\" + fileName;
System.IO.File.WriteAllBytes(filePath, fileContent);
return true;
}
catch (Exception)
{
return false;
}
}
客户端Base64字符串生成
public string ConvertFileToBase64String(string fileName)
{
byte[] fileContent = System.IO.File.ReadAllBytes(fileName);
return Convert.ToBase64String(fileContent);
}
使用上面的方法将文件转换为字符串并将其作为字符串而不是字节数组发送到Web服务