Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
So i have a ASP.NET web app in c#. I've create a method to download files and it works fine. Currently, it's donwloading files in D drive as i pointed out that path. However, i want the files to be downloaded in Local Pictures Library.
Below are my codes:

What I have tried:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Images/") + e.CommandArgument);
WebClient webClient = new WebClient();
webClient.DownloadFile(Server.MapPath("~/Images/") + e.CommandArgument, @"d:\myfile.jpg");
Response.End();


}
}
Posted
Updated 12-Feb-19 4:08am
Comments
Richard MacCutchan 11-Feb-19 6:41am    
Change the destination name to the location you need.

You could look into Environment.SpecialFolders:

Environment.SpecialFolder Enum (System) | Microsoft Docs[^]

There's quite a detailed write up here: Getting All "Special Folders" in .NET[^]
 
Share this answer
 
Quote:
WebClient webClient = new WebClient();
webClient.DownloadFile(Server.MapPath("~/Images/") + e.CommandArgument, @"d:\myfile.jpg");

That code is running on the server. It is copying the image to the root of drive D: on the server.

It might appear to work when you debug the code in Visual Studio. But that's only because, in that specific case, the client and the server are the same computer.

As soon as you deploy your code to a real server, you'll see that the file is being saved on the server, not on the client.

The rest of your code is the only way to send a file to the client:
Response.Clear();
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Images/") + e.CommandArgument);
Response.End();

NB: The user will be prompted to select where they want to save the file. There is no way to control where, or even if, the user saves the file.
 
Share this answer
 

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