Using one method for button click event

时间:2015-06-30 13:48:46

标签: c# asp.net

So I have a website which I am currently working on. There are several pages which will included GridView where users will be able to download files. Currently I am writing the download method for on each page. Is there a better way to achieve this? i.e. call this method on each click event

I am using the below code

protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(filePath));
    Response.ContentType = "application/pdf";
    Response.WriteFile(filePath);
    Response.End();
}

4 个答案:

答案 0 :(得分:4)

我可能会使用通用处理程序。如果这是一个我想缩短代码的小项目,你可以考虑一个简单的扩展方法。

方法1:通用处理程序

将下载内容写入通用处理程序。下面的代码可以是" DownloadPdf.ashx"在你的网站上。

public class DownloadPdf : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string filePath = Uri.UnescapeDataString(context.Request.QueryString["file"]);
        string contentType = Uri.UnescapeDataString(context.Request.QueryString["type"]);
        context.Response.ContentType = contentType;
        context.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(filePath));
        context.Response.ContentType = "application/pdf";
        context.Response.WriteFile(filePath);
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

然后,在你的事件处理程序中:

    protected void DownloadFile(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        string url = string.Format("~/DownloadPdf.ashx?contentType={0}&file={1}", Uri.EscapeDataString(ContentType), Uri.EscapeDataString(filePath));
        Response.Redirect(url);
    }

方法2:扩展程序

如果您只想缩短简单项目的代码,可以编写扩展方法。

public static class Extensions
{
    public static void WritePdfDownload(this HttpResponse response, string filePath, string contentType)
    {
        response.ContentType = contentType;
        response.AppendHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(filePath));
        response.ContentType = "application/pdf";
        response.WriteFile(filePath);
        response.End();
    }
}

然后,您的事件处理程序非常简单:

    protected void DownloadFile(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;
        Response.WritePdfDownload(filePath, ContentType);
    }

答案 1 :(得分:1)

Create a class which you can access from all pages that should be able to Download Files, then in the "onclick()" simply invoke that class.

答案 2 :(得分:0)

Write a DownloadFile method in a Static Download class

Replace the "object Response" in the method signature with the type of your Response object.

namespace DownloadMethods
{
    internal static class Downloads
    {
        public static void DownloadFile(object sender, object Response)
        {
             string filePath = (sender as LinkButton).CommandArgument;
             Response.ContentType = ContentType;
             Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(filePath));
             Response.ContentType = "application/pdf";
             Response.WriteFile(filePath);
             Response.End();
         }
    }
}

The you can just call this:

protected void DownloadFile(object sender, EventArgs e)
{
    DownloadMethods.Downloads.DownloadFile(sender, Response);
}

答案 3 :(得分:0)

If you are having common logic on the pages, maybe you should consider creating base class with download method, and then just inherit this base class for additional pages.

You would have one implementation of the download method, which would make it easier to maintain.

You could also extract the method that downloads the file into separate class and call if from your pages, however as it looks now, you would need to figure out how to pass other dependencies there, like Response object.