2016-12-28 121 views
0
request = MakeConnection(uri, WebRequestMethods.Ftp.DownloadFile, username, password); 
response = (FtpWebResponse)request.GetResponse(); 
Stream responseStream = response.GetResponseStream(); 

//This part of the code is used to write the read content from the server 
using (StreamReader responseReader = new StreamReader(responseStream)) 
{ 
    using (var destinationStream = new FileStream(toFilenameToWrite, FileMode.Create)) 
    { 
     byte[] fileContents = Encoding.UTF8.GetBytes(responseReader.ReadToEnd()); 
     destinationStream.Write(fileContents, 0, fileContents.Length); 
    } 
} 
//This part of the code is used to write the read content from the server 
using (var destinationStream = new FileStream(toFilenameToWrite, FileMode.Create)) 
{ 
    long length = response.ContentLength; 
    int bufferSize = 2048; 
    int readCount; 
    byte[] buffer = new byte[2048]; 
    readCount = responseStream.Read(buffer, 0, bufferSize); 
    while (readCount > 0) 
    { 
     destinationStream.Write(buffer, 0, readCount); 
     readCount = responseStream.Read(buffer, 0, bufferSize); 
    } 
} 

前者的人將內容寫入到文件,但是當我嘗試打開它說,它已損壞的文件。但後來的人在下載zip文件時完成這項工作。是否有任何特定的原因,爲什麼前代碼不適用於zip文件,因爲它適用於文本文件?Zip文件越來越損壞

+0

的StreamReader是一個TextReader的。看[這個答案](http://stackoverflow.com/a/4769070/60761)。 –

回答

0

使用BinaryWriter並將其傳遞給FileStream。

//This part of the code is used to write the read content from the server 
    using (var destinationStream = new BinaryWriter(new FileStream(toFilenameToWrite, FileMode.Create))) 
    { 
     long length = response.ContentLength; 
     int bufferSize = 2048; 
     int readCount; 
     byte[] buffer = new byte[2048]; 
     readCount = responseStream.Read(buffer, 0, bufferSize); 
     while (readCount > 0) 
     { 
      destinationStream.Write(buffer, 0, readCount); 
      readCount = responseStream.Read(buffer, 0, bufferSize); 
     } 
    } 
+0

我試過但結果是一樣的 – Prabu

+0

郵編仍然是腐敗? – Nino

+0

是的,該文件仍然損壞 – Prabu

2
byte[] fileContents = Encoding.UTF8.GetBytes(responseReader.ReadToEnd()); 

你試圖解釋一個二進制PDF文件作爲UTF-8文本。這是行不通的。請參閱Upload and download a binary file to/from FTP server in C#/.NET

+0

我已經完全忽略了......但第二種方法應該工作(使用的BinaryWriter寫入流時)。 – Nino

+0

我明白你的第二個代碼工作(*「但下載的zip文件時,後來的一個完美的做工作」 *)。不是嗎? –

+0

呀後來一個下載完美。 – Prabu

0

,這裏是我的解決方案,爲我工作

C#

public IActionResult GetZip([FromBody] List<DocumentAndSourceDto> documents) 
{ 
    List<Document> listOfDocuments = new List<Document>(); 

    foreach (DocumentAndSourceDto doc in documents) 
     listOfDocuments.Add(_documentService.GetDocumentWithServerPath(doc.Id)); 

    using (var ms = new MemoryStream()) 
    { 
     using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true)) 
     { 
      foreach (var attachment in listOfDocuments) 
      { 
       var entry = zipArchive.CreateEntry(attachment.FileName); 

       using (var fileStream = new FileStream(attachment.FilePath, FileMode.Open)) 
       using (var entryStream = entry.Open()) 
       { 
        fileStream.CopyTo(entryStream); 
       } 
      } 

     } 
     ms.Position = 0; 
     return File(ms.ToArray(), "application/zip"); 
    } 

    throw new ErrorException("Can't zip files"); 
} 

不要錯過ms.Position = 0;這裏