多个用户的内存流性能问题

时间:2019-03-27 15:27:00

标签: c# asp.net-mvc pdf

我的应用程序中有一个部分向用户显示PDF。我正在使用memorystream获取此文档。每当有多个用户同时请求文档时,我的应用程序的性能就会降低。如果我有30个以上的用户,那就来抓取。我的大多数用户都说,当他们到达文档时,它开始变慢。这是我叫文件的方式

public FileStreamResult GetDocument(bool useDefault)
    {
        string contentType = string.Empty;
        short verificationType = VerificationType();
        MemoryStream ms = DocumentToStream(useDefault, out contentType);
        if (contentType == string.Empty) contentType = "application/pdf";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType = contentType;
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline;" + FormFileName());
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
        HttpContext.Current.Response.OutputStream.Flush();
        HttpContext.Current.Response.OutputStream.Close();
        HttpContext.Current.Response.End();
        ms.Close();
        if (verificationType == OBDocVerification.Presentation) SetVerified(verificationType);
        return new FileStreamResult(HttpContext.Current.Response.OutputStream, contentType);
    }

要获取实际文件,它会执行此操作

private MemoryStream CreatePdfStream(PdfReader pdfDoc, List<MappedField> pdfFields, bool useVerifyButton, bool isLocked, bool isI9, bool isManualUpdate, string state) // 04/26/2018 DS TFS # 3161
    {
        using (MemoryStream stream = new MemoryStream())
        {
            PdfStamper stamper = new PdfStamper(pdfDoc, stream);
            if (!isLocked)
            {
                foreach (MappedField mappedFld in pdfFields)
                {
                    if (!string.IsNullOrEmpty(mappedFld.DB_Table))
                    {
                        //string v = PDFformFieldValue(mappedFld, this.docLevel);
                        string v = PDFformFieldValue(mappedFld, this.docLevel, isI9, state); // 10/04/2017 DS TFS # 2768 (added isI9)
                        if (!string.IsNullOrEmpty(v))
                        {
                            stamper.AcroFields.SetField(mappedFld.FormName, v);
                        }
                        else
                        {
                            stamper.AcroFields.SetField(mappedFld.FormName, string.Empty);
                        }
                        if (useVerifyButton)
                        {
                            if (!IsPDFformFieldEditable(mappedFld) || !GlobalVariables.IsIE) stamper.AcroFields.SetFieldProperty(mappedFld.FormName, "setfflags", PdfFormField.FF_READ_ONLY, null);
                        }
                    }
                }
                if (isI9) ValidateI9NAFields(ref stamper, pdfFields);
                if (!isManualUpdate && GlobalVariables.IsIE) stamper.FormFlattening = true; // 04/26/2018 DS TFS # 3161
            }
            //else
            //    stamper.FormFlattening = true;
            if (useVerifyButton && GlobalVariables.IsIE)
            {
                // Add "Verify" button
                string alignmentType;
                int numberOfPages = pdfDoc.NumberOfPages;
                int stampPage = GetVerifyButtonLocation(out alignmentType);
                if (stampPage <= 1) stampPage = numberOfPages;
                if (stampPage > numberOfPages) stampPage = numberOfPages;
                if (alignmentType == string.Empty) alignmentType = "bottom_right";
                Rectangle thePage = pdfDoc.GetCropBox(stampPage);
                float buttonWidth = 100;
                float buttonHeight = 40;
                Rectangle ButtonRect = CreateVerifyButtonLocation(thePage, alignmentType, buttonWidth, buttonHeight);
                PushbuttonField button = new PushbuttonField(stamper.Writer, ButtonRect, "postSubmit");
                button.BackgroundColor = ButtonColor();
                button.BorderColor = GrayColor.BLACK;
                button.BorderWidth = 1f;
                button.BorderStyle = PdfBorderDictionary.STYLE_INSET;
                button.TextColor = BaseColor.WHITE;
                button.FontSize = 12f;
                button.Text = VerifyButtonTitle();
                button.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
                button.Rotation = pdfDoc.GetPageRotation(stampPage);
                PdfFormField field = button.Field;
                //field.Put(PdfName.TU, new PdfString("Save changes and return to the folder."));
                field.Action = PdfAction.CreateSubmitForm(this.submitUrl, null, PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_INCLUDE_NO_VALUE_FIELDS);
                stamper.AddAnnotation(field, stampPage);
            }
            //else
            //    stamper.FormFlattening = true;
            stamper.Close();
            return stream;
        }
    }

我觉得我在这里做的事情效率低下。

1 个答案:

答案 0 :(得分:0)

这根本不是内存流问题。考虑存储生成的流,然后直接将其提供。如果您有一个很大的pdf文件,并且有多个用户,  您的代码将执行多次,结果相同。