Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to directly download and save a file on click of a link...please help
Posted
Comments
Prasad Khandekar 1-Nov-13 6:06am    
It's not clear whether you are talking about an ASP.NET application or Windows Forms application. Please clarify and elaborate the problem.

Regards,

1 solution

You can't specifically save the file if the user clicks on a link: the browser will obey whatever rules the user has set up, including any which require a confirmation.

If you mean "how do I give a user a link to click on, and it downloads a file?" then it depends on a number of factors:
1) Is the file in an available folder relative to the website? If so, then you can just give a line to the file:
HTML
<a href="http://MyDomain.com/folder/file.txt">Click here to download!</a>

2) Is the file stored in a database? If so, then give a link similar to the above, but with a file reference via an ASPX page:
HTML
<a href="wm5ftdl.aspx?file=2552129f-4629-4432-9de3-8177870d81be" target="_blank">Click here to download!</a>
Then in the page, retrieve the data from the DB:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wm5ftdl.aspx.cs" Inherits="wm5ftdl" %>

<%  
    // Send a download file to the client given the filename.    
    string guid = Request.QueryString["file"];
    string fileName = "ERROR";
    byte[] data = new byte[] { 0, 0, 0, 0 };
    string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
        {
        con.Open();
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
                        "FROM dlContent cn " +

                        "WHERE cn.iD=@ID";
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
            {
            cmd.Parameters.AddWithValue("@ID", guid);
            using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                if (r.Read())
                    {
                    fileName = (string) r["filename"];
                    data = (byte[]) r["dataContent"];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Content-Description", "File Download");
    Response.AddHeader("Content-Type", "application/force-download");
    Response.AddHeader("Content-Transfer-Encoding", "binary\n");
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(data);
    //Response.WriteFile("wm5ftdata/" + fileName);
    Response.End();  
%>

There is nothing much in the .CS file:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class wm5ftdl : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
 
Share this answer
 
Comments
Member 10374251 1-Nov-13 6:47am    
actually my mistake...I want this to be done in windows application
..please help
OriginalGriff 1-Nov-13 6:53am    
So what is the problem: just call the appropriate file save method - or am I missing something? Remember, I can't see your screen :laugh:
Member 10374251 1-Nov-13 6:50am    
please help if possible..:(
BillWoodruff 1-Nov-13 11:04am    
Help yourself by taking the time to clarify your question.

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