如何在Api控制器内生成HttpResponseMessage中的zip文件

时间:2018-01-15 17:56:26

标签: c# asp.net-web-api

我无法在web api控制器中生成zip文件。调用可以下载的方法时,我需要生成一个zip文件。 zip文件纯粹是为了下载。 zip文件不应存在于服务器上。 zip文件的内容是服务器上存在的pdf文件数组。我尝试了以下内容,但我不确定如何正确填充HttpResponseMessage内容对象。

有人可以建议你这样做吗?

谢谢

我的失败尝试

using (FileStream zipFile = new FileStream("??", FileMode.Create))
{
    using (ZipArchive archive = new ZipArchive((zipFile, ZipArchiveMode.Create))
    {
        foreach (var pFile in listOfPdfFilenames)
        {
            var filePath = _pdfPathProvider.GetFullPath(pFile);

            if (File.Exists(filePath))
            {
                archive.CreateEntryFromFile(filePath, filePath);
            }
        }

        var result = new HttpResponseMessage(HttpStatusCode.OK);

        // Not sure how to populate the content object with my zipped file
        result.Content = new Content(..);  // Or ByteArrayContent(..)

        return result;
    }

看了问题#45172328中提供的示例,我得到“zip文件为空”。在这次更改后我提供了一个示例。

第二次尝试

var model = new List<FileModel>();

foreach (var pFile in listOfPdfFilenames)
{
    var filePath = _pdfPathProvider.GetFullPath(pFile);

    if (File.Exists(filePath))
    {
        using (FileStream stream = File.Open(filePath, FileMode.Open))
        {
            model.Add(new FileModel
            {
                FileName = filePath,
                FileContent = ReadToEnd(stream)
            });                    
        }

        var archiveStream = model.Compress();
        var content = new StreamContent(archiveStream);
        var response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = content;
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = "fileexport.zip"
        };

        return response;

ReadToEnd方法的实现

    public static byte[] ReadToEnd(System.IO.Stream stream)
    {
        long originalPosition = 0;

        if (stream.CanSeek)
        {
            originalPosition = stream.Position;
            stream.Position = 0;
        }

        try
        {
            byte[] readBuffer = new byte[4096];

            int totalBytesRead = 0;
            int bytesRead;

            while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
            {
                totalBytesRead += bytesRead;

                if (totalBytesRead == readBuffer.Length)
                {
                    int nextByte = stream.ReadByte();
                    if (nextByte != -1)
                    {
                        byte[] temp = new byte[readBuffer.Length * 2];
                        Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                        Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                        readBuffer = temp;
                        totalBytesRead++;
                    }
                }
            }

            byte[] buffer = readBuffer;
            if (readBuffer.Length != totalBytesRead)
            {
                buffer = new byte[totalBytesRead];
                Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
            }
            return buffer;
        }
        finally
        {
            if (stream.CanSeek)
            {
                stream.Position = originalPosition;
            }
        }
    }

清空zip文件消息

error message received when opening the zip file

0 个答案:

没有答案