C# 如何在C中下载按钮onClick事件的文件?

C# 如何在C中下载按钮onClick事件的文件?,c#,download,windows-forms-designer,C#,Download,Windows Forms Designer,我正在尝试下载一个关于我的按钮在C中的onClick事件的文件。有人知道这方面的信息吗 好的,对于那些希望我成为visual studio 2015 windows forms c中一名更具体的即时通讯员的人来说。如果您使用的是MVC,那么您可以尝试以下代码,它对我有用: #region Download File ==> public ActionResult downloadfile(string Filename, string MIMEType) {

我正在尝试下载一个关于我的按钮在C中的onClick事件的文件。有人知道这方面的信息吗


好的,对于那些希望我成为visual studio 2015 windows forms c中一名更具体的即时通讯员的人来说。

如果您使用的是MVC,那么您可以尝试以下代码,它对我有用:

#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
如果您未使用MVC,请参考以下代码:

#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

希望对您有所帮助。

首先,问题太宽泛,未指定是桌面还是Web应用程序。我们建议您指的是Winforms,然后快速回答是DownloadFileAsync Async,以保持用户界面的交互:

  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");
      }
   }
最好有一个进度条:

   void wc_DownloadProgressChanged(object sender,  DownloadProgressChangedEventArgs e)
   {
      progressBar.Value = e.ProgressPercentage;
   }

使用ASP.NET MVC,我在控制器中使用以下命令从content/Audiofile文件夹返回文件

 /// <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);
 }

这看起来像是假设OP使用的是MVC,没有迹象表明您是对的。代码是针对MVC的,带有ActionResult和return视图,但在try块中,有文件下载代码,ASP.net程序员可以理解。感谢m8,这也很好地使用了进度条。