簡體   English   中英

如何在 C# 中的按鈕的 onClick 事件上下載文件?

[英]How can i download a file on a button's onClick event in C#?

我試圖在 C# 中我的按鈕的 onClick 事件上下載一個文件,有人知道這方面的任何信息嗎?

好的,對於那些希望我在 Visual Studio 2015 windows 窗體 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(異步以保持用戶界面的交互性):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM