2014-09-30 40 views
1

我有一個C#窗體窗體應用程序,它從一個url下載文件(asp.net應用程序),但它沒有返回完整的圖像可以說圖像是780kb窗體創建的文件是381字節。無法從服務器獲取完整圖像

我無法弄清楚這個問題。請幫忙。

我使用下載的代碼是:

public bool getFileFromURL(string url, string filename) 
     { 
      long contentLength = 0; 
      Stream stream = null; 
      try 
      { 
       WebRequest req = WebRequest.Create(url); 
       WebResponse response = req.GetResponse(); 
       stream = response.GetResponseStream(); 
       contentLength = response.ContentLength; 


       // Transfer the file 
       byte[] buffer = new byte[10 * 1024]; // 50KB at a time 
       int numBytesRead = 0; 
       long totalBytesRead = 0; 
       using (FileStream fileStream = new FileStream(filename, FileMode.Create)) 
       { 
        using (BinaryWriter fileWriter = new BinaryWriter(fileStream)) 
        { 
         while (stream.CanRead) 
         { 
          numBytesRead = stream.Read(buffer, 0, buffer.Length); 
          if (numBytesRead == 0) break; 
          totalBytesRead += numBytesRead; 
          fileWriter.Write(buffer, 0, numBytesRead); 
         } 
         fileWriter.Close(); 
        } 
        fileStream.Close(); 
       } 
       stream.Close(); 
       response.Close(); 
       req.Abort(); 

       return true; 

      } 
      catch (Exception) 
      { 

       return false; 
      } 



     } 

這是我的asp.net應用程序的代碼:

using (PortalEntities db = new PortalEntities()) 
     { 
       PortalModel.Command command = db.Commands.SingleOrDefault(c => c.id == id); 

       var filePath = Server.MapPath("~/Uploads/"+command.arguments); 
       if (!File.Exists(filePath)) 
        return; 

       var fileInfo = new System.IO.FileInfo(filePath); 
       Response.ContentType = "image/jpg"; 
       Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath)); 
       Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
       Response.WriteFile(filePath); 
       Response.End(); 
     } 
+0

您正在使用哪個版本的.NET? – 2014-09-30 14:05:32

+0

4.0 ............. – 2014-09-30 14:14:35

+0

你調試了你的代碼嗎?該方法結束時'totalBytesRead'的值是多少? – 2014-09-30 14:39:25

回答

1

這是一個可怕的很多代碼來寫一些字節了從網絡響應文件被替換。怎麼樣這樣的事情(.NET 4 +):

public static bool GetFileFromURL(string url, string filename) 
{ 
    try 
    { 
     var req = WebRequest.Create(url); 
     using (Stream output = File.OpenWrite(filename)) 
     using (WebResponse res = req.GetResponse()) 
     using (Stream s = res.GetResponseStream()) 
      s.CopyTo(output); 
     return true; 
    } 
    catch 
    { 
     return false; 
    } 
} 
+0

我收到的文件仍然是381字節。 asp.net代碼可能有問題嗎? – 2014-09-30 14:40:04

+1

你沒有磁盤空間嗎?你是否從上述方法中獲得任何異常(嘗試刪除'try/catch'塊)? – 2014-09-30 14:51:23

+0

沒有例外。 ....也沒有用完空間。 – 2014-09-30 14:53:01

0

您可以下載圖像以更優雅的方式,它之前在這裏討論Unable to locate FromStream in Image class

並使用File.WriteAllBytes方法將字節數組保存爲文件,更多信息請參考 http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes(v=vs.110).aspx

因此,所有客戶端代碼可以

public void getFileFromURL(string url, string filename) 
{ 
    using (var webClient = new WebClient()) 
    { 
     File.WriteAllBytes(filename,webClient.DownloadData(url)); 
    } 
} 
+0

同樣的錯誤。我收到的文件仍然是381字節。 asp.net代碼可能有問題嗎? – 2014-09-30 14:47:52

0

老兄,你爲什麼不使用WebClient.DownloadFileAsync?

private void DownloadFile(string url, string path) 
{ 
    using (var client = new System.Net.WebClient()) 
    { 
     client.DownloadFileAsync(new Uri(url), path); 
    } 
} 

這幾乎是它,但這種方法不能下載超過2GB。但我不認爲這個圖像就是那個大xD。

希望它有幫助!