Why doesn't IIS release files on the server?

2019-08-15 05:51发布

I have ASP.NET Web Forms application for fill e-warranty pdf. When I open file for first time and fill filds everything is OK. But if I want to open file again and fill it, throw me exception on File.WriteAllBytes(pdfDirectory, pdfContent);:

System.IO.IOException: The process cannot access the file '...\AllWarranties\2016\2\000101.pdf' because it is being used by another process.

If I open another file, make changes on them, and return to first one then dont'throw me exception.

Why doesn't IIS release files on the server? On local machine works perfect.

Code

string pdfDirectory = @"C:\Projects\Amco\EWarranty\EWarranty" + currentFilePath.FilePath;
MemoryStream inputMemoryStream = new MemoryStream();

using (FileStream fs = File.OpenRead(pdfDirectory))
{
    inputMemoryStream.SetLength(fs.Length);
    fs.Read(inputMemoryStream.GetBuffer(), 0, (int)fs.Length);
    inputMemoryStream.Seek(0, SeekOrigin.Begin);
}

PdfReader pdfReader = new PdfReader(inputMemoryStream);
using (Stream inputImageStream = new FileStream(@"C:\Projects\Amco\EWarranty\pechatAMCO.png", FileMode.Open, FileAccess.Read, FileShare.Read))
using (MemoryStream outputStream = new MemoryStream())
{
    using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
    {
        if (currentServiceMap.FailureNumber == 0)
        {
            var pdfContentByte = pdfStamper.GetOverContent(3);
            Image image = Image.GetInstance(inputImageStream);
            image.ScaleToFit(150, 150);
            image.SetAbsolutePosition(140, 425);
            pdfContentByte.AddImage(image);
        }

        // Some other else if statements ...

        AcroFields pdfFormFields = pdfStamper.AcroFields;
        BaseFont cyrillicFont = BaseFont.CreateFont(@"C:\Windows\Fonts\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        pdfFormFields.AddSubstitutionFont(cyrillicFont);

        // first fail
        if (txt_First_Adoption_Date.Text != "")
        {
            pdfFormFields.SetField("firstAdoptionDate", txt_First_Adoption_Date.Text);
        }

        if (txt_First_Failure.Text != "")
        {
            pdfFormFields.SetField("firstFailure", txt_First_Failure.Text);
        }

        if (txt_First_Return_Date.Text != "")
        {
            pdfFormFields.SetField("firstReturnDate", txt_First_Return_Date.Text);
        }

        // Second, third and so on failds ...

            warrantyService.UpdateServiceMapByAdmin(CurrentSessions.warrantyNumber, txt_First_Adoption_Date.Text, txt_First_Failure.Text, "", txt_First_Return_Date.Text, txt_Second_Adoption_Date.Text, txt_Second_Failure.Text,
                                                        "", txt_Second_Return_Date.Text, txt_Third_Adoption_Date.Text, txt_Third_Failure.Text, "", txt_Third_Return_Date.Text, txt_Fourth_Adoption_Date.Text, txt_Fourth_Failure.Text,
                                                        "", txt_Fourth_Return_Date.Text, txt_Fifth_Adoption_Date.Text, txt_Fifth_Failure.Text, "", txt_Fifth_Return_Date.Text, (currentServiceMap.FailureNumber + 1));

        pdfStamper.FormFlattening = false;
    }

    byte[] pdfContent = outputStream.ToArray();
    File.WriteAllBytes(pdfDirectory, pdfContent);
}

pdfReader.Close();
inputMemoryStream.Close();

标签: asp.net file iis
0条回答
登录 后发表回答