2010-07-02 102 views
1

我想使用c#web服務將圖像上傳到IIS服務器。我寫這個Web方法如下:使用c#web服務從android應用程序上傳到服務器的圖像

[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方法正在論證爲字節[]。我有轉換的SD卡圖像爲byte [],但是當我把它當作一個URL paramete它不工作。我也嘗試通過將byte []數組轉換爲base64 strig,但它仍然無法正常工作。

任何人都可以告訴我如何使用c#web服務將圖像上傳到IIS服務器。

回答

4

你需要POST它到服務器,設置內容類型和包括字節數組作爲帖子的主體。

C#here做它適用於Android的一個例子的例子見here

+0

非常感謝Andy ... 該代碼工作正常,但上傳後我無法找到上傳圖片的目標文件夾。我在IIS服務器上運行php。 – Sujit 2010-07-07 04:57:45

+0

很高興幫助。上傳的文件應該位於TransientStorage文件夾下的Web服務應用程序文件夾中?文件夾是否存在?你是否收到任何返回的錯誤?你能夠調試Web服務嗎?我也有點困惑,PHP進入這個設置? – 2010-07-07 12:33:41

+0

感謝您的鏈接,它非常有用。我明白如何發送字節[],我在哪裏發送文件名? 爲什麼我們要做這段代碼 outputStream.writeBytes(twoHyphens + boundary + lineEnd);outputStream.writeBytes(「Content-Disposition:form-data; name = \」uploadedfile \「; filename = \」「+ pathToOurFile +」\「」+ lineEnd); outputStream.writeBytes(lineEnd); outputStream.writeBytes(lineEnd); 如果我有我的形象內容什麼是最好的方式發送它? – 2013-05-04 10:20:04

相關問題