2014-09-25 89 views
2

我有來自Android的上傳圖像文件在ASP淨C#遠程web服務的問題。上傳圖像回空文件

的問題是,從Android電子發送到遠程服務器的圖像文件是零千字節;上傳工作,在遠程服務器上正確的文件夾中上傳的圖像文件名是正確的,我沒有在調試Android和調試asp網絡錯誤,但這個圖像是空的。

我將不勝感激您的幫助。

在此先感謝

的Android代碼的Java類:

File mFile = new File("/storage/emulated/0/image_file.jpg"); 
    if(mFile.exists()){ 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     Bitmap bmp = BitmapFactory.decodeFile(mFile.getAbsolutePath()); 
     bmp.recycle(); 
     bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 
     Request.addProperty("byteArray", byteArray); 
    } 

在ASP.NET的C#的WebService

[WebMethod] 
public string sendUpload(Byte[] byteArray, string fileName) 
{ 
    string Path; 
    Path = "D:\\Inetpub\\wwwroot\\myAppAndroid\\uploads\\" + fileName; 
    FileStream objfilestream = new FileStream(Path, FileMode.Create, 
               FileAccess.ReadWrite); 
    objfilestream.Close();  

    return byteArray + "; " + fileName.ToString(); 
} 

首先編輯的問題,我有錯誤在這行

file.Write(byteArray, 0, byteArray.Length); 

[WebMethod] 
public string sendUpload(Byte[] byteArray, string fileName) 
{ 
    string strdocPath; 
    strdocPath = "D:\\Inetpub\\wwwroot\\myAppAndroid\\uploads\\" + fileName; 

    System.IO.FileStream file = System.IO.File.Create(HttpContext.Current.Server.MapPath(".\\myAppAndroid\\uploads\\" + fileName)); 

    file.Write(byteArray, 0, byteArray.Length); 
    file.Close(); 

    return byteArray + "; " + fileName.ToString(); 
} 

回答

0

你sh烏爾德寫接收到的文件的文件系統上的內容:

[WebMethod] 
    public string sendUpload(Byte[] byteArray, string fileName) 
    { 
     string Path; 
     Path = "D:\\Inetpub\\wwwroot\\myAppAndroid\\uploads\\" + fileName; 
     FileStream objfilestream = new FileStream(Path, FileMode.Create, 
                FileAccess.ReadWrite); 
// here some code is missing 
     objfilestream.Close();  

     return byteArray + "; " + fileName.ToString(); 
    } 

你有Byte[]作爲輸入,你從來沒有在服務器磁盤上寫。

+0

如果他從來沒有使用過,他會發現他自己的錯誤,因爲VS發生標記未使用的變量,並給予警告。但他用它,只是不寫它的文件系統,但它返回到用戶(@ChevyMarkSunderland:你爲什麼要像一個MEG發回給用戶時,他只是把它發送給你 - 他會一直保持一個副本,不需要回...;)) – Alexander 2014-09-25 08:40:23

+0

謝謝你,但如果問題是從Android或web服務... – 2014-09-25 08:45:03

+0

造成@ChevyMarkSunderland您的Web服務的C#代碼不是寫文件的內容,我不明白在文件系統上。如果您不在文件系統上寫入內容,則文件將始終爲空。你必須寫東西到該文件中沒有空文件... – Paolo 2014-09-25 08:48:33