With the use of the following code you can give the user the ability to download files to any location on their computer.
To download a file I am creating a function that checks a file's type and downloads the file from their location. After clicking on the download button a save dialog box is used for saving the downloaded file.
This is the function:
private void DownloadFile(string fname, bool forceDownload)
{
string Filepath = fname;
string Filename = Path.GetFileName(Filepath);
string ext = Path.GetExtension(Filepath);
string type = "";
// set known types based on file extension
if (ext != null)
{
switch (ext.ToLower())
{
case ".htm":
case ".html":
type = "text/HTML";
break;
case ".txt":
type = "text/plain";
break;
case ".doc":
case ".docx":
case ".rtf":
type = "Application/msword";
break;
case ".wav":
case ".Mp3":
type = "Application/media";
break;
}
}
if (forceDownload)
{
Response.AppendHeader("content-disposition",
"attachment; filename=" + Filename);
}
if (type != "")
Response.ContentType = type;
Response.WriteFile(Filepath);
Response.End();
}
if (forceDownload)
{
Response.AppendHeader("content-disposition",
"attachment; filename=" + Filename);
}
if (type != "")
Response.ContentType = Filetype;
Response.WriteFile(Filepath);
Response.End();
}
To force a download you have to pass true there.
Filepath is showing the path of the file from where we are downloading a file.