简体   繁体   English

在ASP.NET中提供静态文件

[英]Serve static file in ASP.NET

I am just wanting to drop a pdf file in a folder in my solution and let users download it on my site. 我只是想将pdf文件拖放到解决方案的文件夹中,然后让用户将其下载到我的网站上。 Pretty simple! 很简单!

I have a main .aspx page that contains a static link to another .aspx page that I am using just for downloading a file. 我有一个.aspx主页面,其中包含指向另一个我只是用于下载文件的.aspx页面的静态链接。 The code works if I run the download page directly out of visual studio however if I run my main page and click the that I have pointing to this page it does not work. 如果我直接从Visual Studio之外运行下载页面,则该代码有效,但是如果运行我的主页并单击指向该页面的,则该代码不起作用。 Here's the code for the download page: 这是下载页面的代码:

FileInfo file = new FileInfo(Server.MapPath("~/Workflow/Workflow v3.pdf"));            
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";;
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.BinaryWrite((byte[])File.ReadAllBytes(Server.MapPath("~/Workflow/Workflow v3.pdf")));
Response.Flush();
Response.End();

And just for reference.. here is another download page I am using in a different area of my tool. 仅供参考,这是我在工具的其他区域使用的另一个下载页面。 This page actually takes a parameter and hits the database to grab files stored in a database. 该页面实际上带有一个参数,并访问数据库以获取存储在数据库中的文件。 This code DOES work, but I don't want to do it this way for my "workflow" download page. 这段代码可以正常工作,但是我不想在我的“工作流程”下载页面上使用这种方法。

        ...
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = AgreementDocumentTable.Rows[0]["ContentType"].ToString();
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + AgreementDocumentTable.Rows[0]["Title"].ToString());
        Response.BinaryWrite((byte[])AgreementDocumentTable.Rows[0]["AgreementDocument"]);
        Response.Flush();
        Response.End();

I was able to find this answer that explains you should not use the Response.End() instead it recommends using CompleteRequest() method. 我能够找到这个答案,说明您不应使用Response.End(),而是建议您使用CompleteRequest()方法。

HttpContext throws HttpException HttpContext抛出HttpException

http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx

Got this working by calling a javascript function and using ScriptManager.RegisterClientScriptBlock 通过调用javascript函数并使用ScriptManager.RegisterClientScriptBlock使此工作正常

This work (don't know 100% why, would like an explanation) so I'm going with it... 这项工作(不知道100%为什么,想要一个解释),所以我要继续下去...

Markup: 标记:

<a runat="server" id="WorkflowDownloadLink" onserverclick="DownloadWorkflowLink_Click" href="">

Event code: 事件代码:

protected void DownloadWorkflowLink_Click(Object sender, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Download", "GotoDownloadPage('./Workflow.aspx');", true);
    }

Code behind on Workflow.aspx: 在Workflow.aspx后面的代码:

protected void Page_Load(object sender, EventArgs e)
    {

        FileInfo file = new FileInfo(Server.MapPath("~/Workflow/Workflow v3.pdf"));

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.BinaryWrite((byte[])File.ReadAllBytes(Server.MapPath("~/Workflow/Workflow v3.pdf")));
        Response.Flush();



    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM