簡體   English   中英

從C#服務器下載后,Zip文件已損壞

[英]Zip file is getting corrupted after downloading from server in C#

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文件?

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

您嘗試將二進制PDF文件解釋為UTF-8文本。 那根本行不通。

有關正確的代碼,請參見在C#/。NET中將二進制文件上傳到FTP服務器或從FTP服務器下載二進制文件

使用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);
        }
    }

這是對我有用的解決方案

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; 這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM