开发者

Downloading an aspx page

开发者 https://www.devze.com 2023-03-09 01:46 出处:网络
In my web app, I have an aspx page which contains an html table and several lines of text. I need users to be able to download this whole page as a separate file.

In my web app, I have an aspx page which contains an html table and several lines of text. I need users to be able to download this whole page as a separate file.

In the past I have used the a webclient to do this:

Dim myWebClient As New System.Net.WebClient
开发者_如何转开发myWebClient.DownloadFile(strSource, strDest)
Response.AddHeader("Content-Disposition", "attachment;filename=test.doc")
Response.Write("test.doc")

but it appears this is only able to download html pages.

Can this be done?


The reason you can only download HTML is because .aspx is not served over the internet (only the resulting HTML is).

When you are doing the myWebClient.DownloadFile() the application is simply making a GET request to the URI and saving the resulting HTML. The .aspx never leaves the server - rather it is processed server side resulting in the HTML you are ending up with.


.ASPX is a scripted page, so you can't actually download the original ASPX. The IIS server on the other end has a handler for .aspx resulting in .NET Processing it. Generally, you don't want a server returning raw ASPX source.

It would require special handling on the server side to be able to get a raw ASPX page. For instance, you could create an ASHX script handler that does it for you, you'd request something like getfile.ashx?filename=myfile.aspx and the getfile.ashx handler would read the ASPX page off the disk and write that as the response. (Security note: If this is the route you choose, make sure to sanitize the page that is specified so they don't do something silly like getfile.ashx?filename=C:\my\secret\file.txt) It would be even better to set the trust level of that handler to medium or lower.

But that all requires serverside development. From the client side, there isn't anything you can do until the server wants to play along.

Here is an example of a file handler:

public class MyHandler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        var file = context.Request.QueryString["file"];
        //Make sure to do your security checks on the file path here.
        using (var source = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            source.CopyTo(context.Response.OutputStream);
        }
    }
}

You can setup the handler either inside of an ASHX or through the httpHandlers section in the web.config.

Or if you are using MVC2+, you don't need an HTTP Handler as you can just use an action to achieve the same thing:

public ActionResult GetFile(string path)
{
    //Make sure to do your security checks
    using (var source = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        return File(source, "text/html");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消