【问题标题】:Pdfs inside zip file are corruptedzip 文件中的 Pdfs 已损坏
【发布时间】:2015-07-25 23:30:44
【问题描述】:

我正在尝试制作一个包含 pdf 的 zip 文件。当我提取 zip 文件时,pdf 文件已损坏。我在“outputStream”上放了一只手表。这是第一个例外

'outputStream.Length' 抛出了一个类型为 'System.NotSupportedException' long 的异常

这是整个手表

代码

[HttpPost]
    [ActionName("ZipFileAction")]
    public HttpResponseMessage ZipFiles([FromBody]int[] id)
    {
        if (id == null)
        {//Required IDs were not provided
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
        }

        List<Document> documents = new List<Document>();
        using (var context = new ApplicationDbContext())
        {
            foreach (int NextDocument in id)
            {
                Document document = context.Documents.Find(NextDocument);

                if (document == null)
                {
                    throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
                }

                documents.Add(document);
            }
            var streamContent = new PushStreamContent((outputStream, httpContext, transportContent) =>
            {
                try
                {
                    using (var zipFile = new ZipFile())
                    {
                        foreach (var d in documents)
                        {
                            var dt = d.DocumentDate.ToString("y").Replace('/', '-').Replace(':', '-');
                            string fileName = String.Format("{0}-{1}-{2}.pdf", dt, d.PipeName, d.LocationAb);
                            zipFile.AddEntry(fileName, d.DocumentUrl);
                        }
                        zipFile.Save(outputStream); //Null Reference Exception
                    }
                }

                finally
                {
                    outputStream.Close();
                }
            });
            streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            streamContent.Headers.ContentDisposition.FileName = "reports.zip";

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = streamContent
            };
            return response;
        }

【问题讨论】:

    标签: c# asp.net-web-api ionic zipfile pushstreamcontent


    【解决方案1】:

    PushStreamContent 用于数据流。我认为您应该创建一个临时 zip 文件,并将其传递给 Response

    示例

    public FileResult ZipFiles(...)
    {
       // create a temporary file
    
        return File("path/to/file.zip", System.Net.Mime.MediaTypeNames.Application.Octet);
    }
    

    【讨论】:

    • 请看示例。另外,this帖子的回复也不错。
    【解决方案2】:

    问题原来是我没有在 pdf 中保存任何内容。我只是保存documentUrl。所以通过使用 FileInfo 来改变事情。工作代码

    [HttpPost]
        [ActionName("ZipFileAction")]
        public HttpResponseMessage ZipFiles([FromBody]int[] id)
        {
            if (id == null)
            {//Required IDs were not provided
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
    
            List<Document> documents = new List<Document>();
            using (var context = new ApplicationDbContext())
            {
                foreach (int NextDocument in id)
                {
                    Document document = context.Documents.Find(NextDocument);
    
                    if (document == null)
                    {
                        throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
                    }
    
                    documents.Add(document);
                }
                var streamContent = new PushStreamContent((outputStream, httpContext, transportContent) =>
                {
                    try
                    {
                        using (var zipFile = new ZipFile())
                        {
                            foreach (var d in documents)
                            {
                                var dt = d.DocumentDate.ToString("y").Replace('/', '-').Replace(':', '-');
                                string fileName = String.Format("{0}-{1}-{2}.pdf", dt, d.PipeName, d.LocationAb);
                                FileInfo fi = new FileInfo(d.DocumentUrl);
                                var fileReadStream = fi.OpenRead();
                                var fileSize = (int)fi.Length;
                                var fileContent = new byte[fileSize];
                                fileReadStream.Read(fileContent, 0, fileSize);
                                zipFile.AddEntry(fileName, fileContent);
                            }
                            zipFile.Save(outputStream); //Null Reference Exception
                        }
                    }
    
                    finally
                    {
                        outputStream.Close();
                    }
                });
                streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                streamContent.Headers.ContentDisposition.FileName = "reports.zip";
    
                var response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = streamContent
                };
                return response;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多