简体   繁体   中英

Cannot download files/images from uploads folder in asp.net

i need to download images that i stored in folder in asp.net web application name as uploads

i got a function that should download this as

private void downloadAnImage(string strImage)
{
    Response.ContentType = "image/jpg";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + strImage);
    Response.TransmitFile(strImage);
    Response.End();
}

and i call this function from link button as

protected void lnkDwnSlc_Click(object sender, EventArgs e)
{
    if (Session["slc_filepath"] != null)
    {                 
         string path = Server.MapPath(Session["slc_filepath"].ToString());
         downloadAnImage(path);
    }
}

where Session["slc_filepath"] is path stored in session

but after running this code file/image is not downloading and I got no error about why file is not downloading. And I checked file path by using breakpoint , it is correct,

I search a lot form google but i can't understand where I missed something.

EDIT: on page load event i pull records from table there i saved path of file and in session i store it like

Session["slc_filepath"] = dt.Rows[0]["UploadSLC"].ToString();

where UploadSLC is column name of table where i store path of image and in database string is looking as

~\uploads\ab071770-473a-4e1a-8cfc-addeccf565d5.jpg

Try this:

private void downloadAnImage(string strImage)
{
    Response.Clear();
    Response.ContentType = "image/jpg";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + strImage);
    Response.TransmitFile(strImage);
    Response.Flush();
    Response.End();
}

Happy to help you!

I suggest to troubleshoot the issue where it came from.

  1. Try a hardcoded path (to make sure, you are passing the correct path)

     //try forward or back slash, make sure the file exists string path = Server.MapPath("~/uploads/ab071770-473a-4e1a-8cfc-addeccf565d5.jpg"); 
  2. Try this:

     private void downloadAnImage(string strImage) { Response.ContentType = "image/jpg"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + strImage); Response.TransmitFile(strImage); Response.Flush(); Response.End(); } 
  3. Make sure there's no javascript error that is being thrown on the client side.

Please correct your path of image while binding from database

like "~/Image/images.png ".

PFB of my snippet code you may supposed to get solution.

protected void Page_Load(object sender, EventArgs e) { Session["slc_filepath"] = "~/Image/images.png"; }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath(Session["slc_filepath"].ToString());
        downloadAnImage(path);
    }

    private void downloadAnImage(string strImage)
    {
        Response.ContentType = "image/jpg";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + strImage);
        Response.TransmitFile(strImage);
        Response.End();
    }

Use an ashx Handler like this http://www.intstrings.com/ramivemula/asp-net/how-to-download-files-from-server-to-client-using-a-generic-handler/

Then send by ajax a window.Open to the new ashx.

I found answer after a long searching but a hint i got is from my junior in my team i really appreciate it, i just added

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
            scriptManager.RegisterPostBackControl(this.lnkDwnSlc); 

in pageLoad event because i am using update panel and script manager in my aspx page and bootstrap for design so it stopped functionality

after doing so it works like a charm,

thanks all of your efforts and support,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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