簡體   English   中英

FileResult 內容長度不匹配

[英]FileResult content-length mismatch

嗨,我正在使用這篇博文中的代碼:

https://blog.stephencleary.com/2016/11/streaming-zip-on-aspnet-core.html

為了使用 .Net 核心流式傳輸 zip 文件。 我讓它工作了,但是由於我在下載 zip 文件時沒有在響應中添加內容長度標頭,所以它不會在 chrome 中顯示下載進度。 由於我事先知道 zip 文件大小,我實際上可以使用 SetHeadersAndLog 方法https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.internal.fileresultexecutorbase設置內容長度標頭.setheadersandlog?view=aspnetcore-2.0

但是當我這樣做時,我有以下錯誤:

System.InvalidOperationException: Response Content-Length mismatch: too many bytes written (144144633 of 144144627)

知道為什么響應的長度與 zip 文件的長度不同嗎? 這是提供文件的代碼:

     this._httpContext.Response.ContentType = "application/octet-stream";
            this._httpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
            this._httpContext.Response.ContentLength = estimatedFileSize;

            FileCallbackResult result = new FileCallbackResult(new MediaTypeHeaderValue("application/octet-stream"), estimatedFileSize, async (outputStream, _) =>
            {
                using (ZipArchive zip = new ZipArchive(outputStream, ZipArchiveMode.Create, false))
                {
                    foreach (string filepath in Directory.EnumerateFiles(existingDirectory.FullName, "*.*", SearchOption.AllDirectories))
                    {
                        string relativepath = filepath.Replace(existingDirectory.FullName + "\\", string.Empty);

                        ZipArchiveEntry zipEntry = zip.CreateEntry(relativepath, CompressionLevel.Fastest);
                        using (Stream zipstream = zipEntry.Open())
                        {
                            using (Stream stream = new FileStream(filepath, FileMode.Open))
                            {
                                await stream.CopyToAsync(zipstream);
                            }
                        }
                    }
                }
            })
            {
                FileDownloadName = $"{package.FileName}.zip",
            };

你需要尋找流回到起點。

暫無
暫無

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

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