Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to download file which is available on my computer.

I have path available :-
E:/Documents/Marketing/2/ComboxCommnets.png


how can i download this file by clcking on link
C#
if (attachments != null && attachments.Count > 0)
                {
                    Literal ltrAttachment = (grdUserMarketingUpdates.FindDetailRowTemplateControl(e.VisibleIndex, "ltrAttachment") as Literal);
                    strAttchments = new StringBuilder();
                    foreach (var item in attachments)
                    {
                        strAttchments.Append("<a style=\"color: blue\" href =" + item.URL + " >" + item.Name + "</ a > <br />");
                    }
                    ltrAttachment.Text = strAttchments.ToString().Trim();
                }
Posted
Updated 26-Nov-15 23:42pm
v2
Comments
[no name] 27-Nov-15 4:09am    
What is the value you are getting in item.URL
Torakami 27-Nov-15 5:05am    
E:/Documents/Marketing/1/ComboxCommnets.png
Torakami 27-Nov-15 5:05am    
This is not my location where my project is present , I am saving outside the project

1 solution

In order to download an image click on a href link follow below steps:

Step1: Create a ASHX generic file to download an image.
C#
public class DownloadFile : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context)
    {
 
        string destPath = context.Server.MapPath(HttpContext.Current.QueryString["path"]);
        // Check to see if file exist
        FileInfo fi = new FileInfo(destPath);
 
        // If the file exist on the server then add it to the database
        if (fi.Exists)
        {
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
            HttpContext.Current.Response.ContentType = "application/image";;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=temp.png");
            HttpContext.Current.Response.WriteFile(destPath);
            HttpContext.Current.Response.End();
        }
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    } 
}

Step2: Now change in code behind where you are adding href link
C#
foreach (var item in attachments)
{
    strAttchments.Append("<a style=\"color: blue\" href ='DownloadFile.ashx?path=" + item.URL + "' >" + item.Name + "</ a > <br />");	
}

Link should look like:
HTML
<a style="color: blue" href="DownloadFile.ashx?path=E:/Documents/Marketing/1/ComboxCommnets.png">Download</a>
 
Share this answer
 
v2
Comments
Torakami 27-Nov-15 6:12am    
let me try this , I will test . but can you tell me what if my file can be of any type . I mean image , or doc , or pdf .. Please suggest
[no name] 27-Nov-15 6:16am    
Then you need to change "ContentType" attribute as per file type(pdf, doc and others). First you need to fetch file extenssion and set contenttype as per file type. For content type it is here[^]

If it helps accept my answer.
Torakami 27-Nov-15 6:21am    
Ok thanks , can you please help me for some more time.
I am getting an error like this

Requested URL: /Home/~/Handlers/DownloadFile.ashx .

My file is present inside handler folder .
[no name] 27-Nov-15 6:23am    
URL should be: /Home/Handlers/DownloadFile.ashx?path=e/test.png

Change in your code to get exact url otherwise it will hit the ashx file.
Torakami 27-Nov-15 6:44am    
Can we have multiple hadler files in one folder , because when i tried to navigate to thi handler filer , its redirecting me to other ones processrequest

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900