Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following code to download a file to client. How to set the download path at clientside to C: drive programatically without user interaction.

Default.aspx

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("download.aspx?file=~/images/aa.txt");
    }


Download.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        string strRequest =(string) Request.QueryString["file"];       
        if (!string.IsNullOrEmpty(strRequest))
        {
            string path = Server.MapPath(strRequest);           
            System.IO.FileInfo file = new System.IO.FileInfo(path);           
            if (file.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.WriteFile(file.FullName);
                Response.End();              
            }
            else
            {
                Response.Write("This file does not exist.");
            }           
        }
        else
        {
            Response.Write("Please provide a file to download.");
        }


    }
Posted
Updated 4-Jul-11 21:32pm
v2
Comments
[no name] 5-Jul-11 3:32am    
Edited for Code Block.

1 solution

You might try specifying a path in the filename variable, but I doubt it will work. Your user will always be asked, because a website can't just write a file to a local user, that would be a huge security hole.

Also, a windows 7 user cannot save files to c:\my file.txt.
 
Share this answer
 
v2
Comments
SHAJANCHERIAN 5-Jul-11 4:21am    
In my application I can give message to user to creater a folder in their PC and to give necessary permissions. Thereafter whenever he download the file from my website it should save to that folder. In this case Can you share your ideas.

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