【问题标题】:Cannot access a closed file .net MVC?无法访问已关闭的文件 .net MVC?
【发布时间】:2017-05-26 02:14:27
【问题描述】:

只想使用 itextsharp 向现有 pdf 添加文本 .到目前为止我已经完成了。我的代码=>

    public FileStreamResult DownloadCertificate(string UserId)
    {
        //get user info using UserId from database
        //UserDetail UserDetail = db.UserDetails.Where(x => x.UserId == UserId).FirstOrDefault();
        string oldFile = Server.MapPath("~/Content/img/tsms/Certificate/Certificate-of-Completion-Award-Template-Blue.pdf");
        string newFile = Server.MapPath("~/Content/img/tsms/Certificate/newFile.pdf");
        // open the reader
        PdfReader reader = new PdfReader(oldFile);
        Rectangle size = reader.GetPageSizeWithRotation(1);
        Document document = new Document(size);
        // open the writer
        FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
        PdfWriter writer = PdfWriter.GetInstance(document, fs);
        document.Open();
        // the pdf content
        PdfContentByte cb = writer.DirectContent;
        // select the font properties
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SetColorFill(BaseColor.DARK_GRAY);
        cb.SetFontAndSize(bf, 8);
        // write the text in the pdf content
        cb.BeginText();
        string text = "Some random blablablabla...";
        // put the alignment and coordinates here
        cb.ShowTextAligned(1, text, 520, 640, 0);
        cb.EndText();
        // write the text in the pdf content
        cb.BeginText();
        text = "Other random blabla...";
        // put the alignment and coordinates here
        cb.ShowTextAligned(2, text, 100, 200, 0);
        cb.EndText();
        // create the new page and add it to the pdf
        PdfImportedPage page = writer.GetImportedPage(reader, 1);
        cb.AddTemplate(page, 0, 0);
        // close the streams and voilá the file should be changed :)
        document.Close();
        fs.Close();
        writer.Close();
        reader.Close();
        return new FileStreamResult(fs, "application/pdf");
    }

问题是当我尝试访问显示如下错误的方法浏览器时=>

我不知道为什么会出现这种错误。

我试图找到解决方案。我发现.... FileStream "cannot access closed file"

但这对我来说还不够。

我还尝试更改代码中的一些行。下面=>

  // close the streams and voilá the file should be changed :)
  document.Close();
  //fs.Close();
  //writer.Close();
  //reader.Close();
  return new FileStreamResult(fs, "application/pdf");

但这种变化也对我没有帮助。我在代码中做错了什么。(以及如何向用户提供下载模式。)

【问题讨论】:

  • 您有两个问题。您已经用更改的块“below=>”回答了问题的第一部分——您在关闭“fs.close”后尝试访问“fs”。您想将 FileStreamResult 保存到本地变量,然后关闭您的文档、文件流等……最后返回本地 FileStreamResult 变量。你的错误信息给了你答案。重新阅读它并查看您的变量名称以及关闭它们的位置并访问它们以获取线索。总帐
  • @mjw 是的,你是对的。现在改变` //document.Close(); //fs.Close(); //writer.Close(); //reader.Close();` 但在那之后它向我显示了另一个错误link 你能说点什么吗...
  • 将您的 FileStream 构造函数调用更改为:FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.ReadWrite);

标签: c# asp.net-mvc itext


【解决方案1】:

在我看来这是 FileStream 的问题。我有根据的猜测是对document.Close(); 的调用也将关闭FileStream

您可以尝试不重用相同的流,而是打开一个新的只读流(顺便说一下,这也会重置其位置)并将其用于结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多