Im trying to download a file on my button's onClick event in C# anyone got any info on this?
Ok so to the people who wanted me to be a litle more specific im working in visual studio 2015 windows forms c#.
If you are using MVC then you can try the following code it is working for me :
#region Download File ==>
public ActionResult downloadfile(string Filename, string MIMEType)
{
try
{
string file_name = "/Files/EvidenceUploads/" + Filename;
string contentType = "";
//Get the physical path to the file.
string FilePath = Server.MapPath(file_name);
string fileExt = Path.GetExtension(file_name);
contentType = MIMEType;
//Set the appropriate ContentType.
Response.ContentType = contentType;
Response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(file_name)).Name);
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
return View(FilePath);
}
catch
{
Response.End();
return View();
//To Do
}
}
#endregion
If you are not using MVC please refer following code:
#region Download File ==>
public void downloadfile(string Filename, string MIMEType)
{
try
{
string file_name = "/Files/EvidenceUploads/" + Filename;
string contentType = "";
//Get the physical path to the file.
string FilePath = Server.MapPath(file_name);
string fileExt = Path.GetExtension(file_name);
contentType = MIMEType;
//Set the appropriate ContentType.
Response.ContentType = contentType;
Response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(file_name)).Name);
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
}
catch
{
Response.End();
//To Do
}
}
#endregion
Hope its help to you.
First, question is too broad (Not specified is that desktop or Web application) . Lets suggest that you mean Winforms, then quick answer is DownloadFileAsync ( Async in order to keep UI interactive for user):
private void Download_Click(object sender, RoutedEventArgs e)
{
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
wc.DownloadFileAsync(new System.Uri("http://url"),
"Result location");
}
}
It`sa good idea to have a progress bar:
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
Using ASP.NET MVC I used the following within a controller to return a file from the content/Audiofile folder.
/// <summary>
/// The download action returns the file as a File download.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
[AllowAnonymous]
public FilePathResult Download(String filename)
{
String myfile = Path.Combine(HttpContext.Server.MapPath("~/Content/audiofiles/"), filename);
return File(myfile, System.Net.Mime.MediaTypeNames.Application.Octet, filename);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.