2011-10-21 36 views

回答

1

正如威爾吳邦國說,你總是可以宣佈一個Web方法,在您的Web服務需要一個byte []作爲輸入,但如果你不喜歡送字節數組,因爲它是在你的Web服務調用,您可以字節[]編碼總是從客戶端一個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服務作爲一個字符串,而不是字節數組

2

如果使用的WebService,一般,我們定義一個特定的webmethod 這需要一個字節數組PARAM和串PARAM如 恥骨空隙UploadFile(字節字節(),文件名as String)

然後,我們可以在.NET應用程序中輕鬆調用它,因爲我們可以使用 WSDL.EXE或VS.NET生成easytouse客戶端代理類。

Reference

+0

說的字節[]在C# –