我怎样才能使一个网页提供给用户下载上生成的文件?(How can I make a file gen

2019-10-24 01:59发布

这个问题关系到一个我问这里的“我怎么能提示用户输入一个从SharePoint页面保存位置?”

我需要做的是,由此生成PDF后:

        private void GeneratePDF(List<ListColumns> listOfListItems)
        {
            . . .
            //Create a stream that we can write to, in this case a MemoryStream
            StringBuilder sb = new StringBuilder();
            using (var ms = new MemoryStream())
            {
                using (var doc = new Document(PageSize.A4, 25, 25, 10, 10)) 
                {
                    //Create a writer that's bound to our PDF abstraction and our stream
                    using (var writer = PdfWriter.GetInstance(doc, ms))
                    {

                        //Open the document for writing
                        doc.Open();

                        Paragraph docTitle = new Paragraph("UCSC - Direct Payment Form", timesBold16UCSCBlue);
                        doc.Add(docTitle);

                        Paragraph subTitle = new Paragraph("(Not to be used for reimbursement of services)", timesRoman9Font);
                        subTitle.IndentationLeft = 20;
                        doc.Add(subTitle);

                        . . . // tons of iTextSharp PDF-generation code elided for brevity

                        String htmlToRenderAsPDF = sb.ToString();

                        //XMLWorker also reads from a TextReader and not directly from a string
                        using (var srHtml = new StringReader(htmlToRenderAsPDF))
                        {
                            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                        }

                        doc.Close();
                    }
                }
                try
                {
                    var bytes = ms.ToArray();
                    // This is currently saved to a location on the server such as C:\Users\TEMP.SP.005\Desktop\iTextSharpTest.pdf (but the number (such as 
"005" irregularly increments))
                    String pdfFileID = GetYYYYMMDDAndUserNameAndAmount();
                    String pdfFileName = String.Format("DirectPayDynamic_{0}.pdf", pdfFileID);
                    String fileFullpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), pdfFileName);
                    String fileLinkBase = "Generated PDF: <a href=\"{0}\">{1}</a>";
                    String filelink = String.Format(fileLinkBase, fileFullpath, pdfFileName);
                    File.WriteAllBytes(fileFullpath, bytes);

                    var pdflink = new Label
                    {
                        CssClass = "finaff-webform-field-label",
                        Text = filelink
                    };
                    this.Controls.Add(pdflink);
                }
                catch (DocumentException dex)
                {
                    throw (dex);
                }
                catch (IOException ioex)
                {
                    throw (ioex);
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
        } // GeneratePDF

...得到用户的机会,该文件保存到本地机器(目前,它被保存在服务器上)。

IOW,我基本上想要做的就是替换这一行:

String fileFullpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), pdfFileName);

...喜欢的东西:

String fileFullpath = "Hey, bloke (or blokette), where do you want to save this?"

这显然是可能的,甚至有些常见的,因为许多网站会为你生成的文件,然后让你保存这些

生成的文件要么你选择一个位置或默认位置,如您的下载文件夹中。

无论是服务器端(C#)或客户端(jQuery的)的解决方案是可接受的,虽然我更喜欢的服务器端,因为这是被生成的PDF是哪里。

Answer 1:

当我不得不强迫Excel电子表格下载到客户端,我用这个代码,它可以指向你,你是想获得:

类型“的LinkBut​​ton”的控制XXX必须放在一个窗体标记内与RUNAT =服务器

protected void ExportExcelFile(object Sender, EventArgs e) { //export to excel
        var grdResults = new DataGrid(); // REF: https://stackoverflow.com/q/28155089/153923
        if (periodCriteria.SelectedValue == "year") {
            grdResults = RollupDG;
        } else {
            grdResults = QuarterDG;
        }
        var response = HttpContext.Current.Response;
        response.Clear();
        response.Charset = String.Empty;
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment; filename=GlBudgetReport.xls");
        using (var sw = new StringWriter()) {
            using (var htw = new HtmlTextWriter(sw)) {
                grdResults.RenderControl(htw);
                response.Write(sw.ToString());
                response.End();
            }
        }
}


文章来源: How can I make a file generated on a web page available to the user as a download?