首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使在网页上生成的文件可供用户下载?

如何使在网页上生成的文件可供用户下载?
EN

Stack Overflow用户
提问于 2015-07-21 16:05:55
回答 1查看 75关注 0票数 1

这个问题与我问here的一个问题有关:“我如何从Sharepoint页面提示用户保存位置?”

我需要做的是,在生成PDF之后:

代码语言:javascript
复制
        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

...afford使用户有机会将该文件保存到他们的本地计算机上(目前,该文件正在服务器上保存)。

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

代码语言:javascript
复制
String fileFullpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), pdfFileName);

...with类似于:

代码语言:javascript
复制
String fileFullpath = "Hey, bloke (or blokette), where do you want to save this?"

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

生成文件到您选择的位置或默认位置(如下载文件夹)。

服务器端(C#)或客户端(jQuery)解决方案都是可以接受的,尽管我更喜欢服务器端,因为这是生成PDF的地方。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-22 21:57:43

当我不得不强制Excel电子表格下载到客户端时,我使用了以下代码,这可能会指向您想要获得的位置:

Control xxx of type 'LinkButton' must be placed inside a form tag with runat=server

代码语言:javascript
复制
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();
            }
        }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31544178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档