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

时间:2016-04-28 11:30:14

标签: c# download windows-forms-designer

我试图在我的按钮上下载一个文件在C#中的onClick事件,任何人都有关于此的任何信息?

好的,对于那些希望我成为视觉工作室2015 windows窗体c#中更具体的人来说。

3 个答案:

答案 0 :(得分:2)

如果您正在使用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

希望对你有所帮助。

答案 1 :(得分:1)

首先,问题太广泛(未指定是桌面或Web应用程序)。让我们建议您的意思是Winforms,然后快速回答是DownloadFileAsync(Async以保持用户的UI交互):

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

答案 2 :(得分:0)

使用ASP.NET MVC我在控制器中使用以下内容从content / Audiofile文件夹返回文件。

INSERT IGNORE INTO `players`(`commonName`, `firstName`, `headshotImgUrl`, `lastName`, `leagueid`, `nationimageUrlssmall`, `nationimageUrlsmedium`, `nationimageUrlslarge`, `nationid`, `clubimageUrlsnormalsmall`, `clubimageUrlsnormalmedium`, `clubimageUrlsnormallarge`, `clubid`, `headshotlargeImgUrl`, `headshotmedImgUrl`, `headshotsmallImgUrl`, `specialImageslargeTOTWImgUrl`, `specialImagesmedTOTWImgUrl`, `position`, `playStyle`, `height`, `weight`, `birthdate`, `age`, `acceleration`, `aggression`, `agility`, `balance`, `ballcontrol`, `foot`, `skillMoves`, `crossing`, `curve`, `dribbling`, `finishing`, `freekickaccuracy`, `gkdiving`, `gkhandling`, `gkkicking`, `gkpositioning`, `gkreflexes`, `headingaccuracy`, `interceptions`, `jumping`, `longpassing`, `longshots`, `marking`, `penalties`, `positioning`, `potential`, `reactions`, `shortpassing`, `shotpower`, `slidingtackle`, `sprintspeed`, `standingtackle`, `stamina`, `strength`, `vision`, `volleys`, `weakFoot`, `traits0`, `traits1`, `traits2`, `traits3`, `specialities0`, `specialities1`, `specialities2`, `specialities3`, `specialities4`, `specialities5`, `specialities6`, `specialities7`, `specialities8`, `atkWorkRate`, `defWorkRate`, `playerType`, `attributes0value`, `attributes1value`, `attributes2value`, `attributes3value`, `attributes4value`, `attributes5value`, `name`, `quality`, `color`, `isGK`, `positionFull`, `discardValue`, `id`, `baseId`, `rating`, `specialities9`, `specialities10`, `traits4`) VALUES ('', '', '', '', , '', '', '', , '', '', '', , '', '', '', '', '', '', '', , , '', , , , , , , '', , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', , , , , , , '', '', '', '', '', '', , , , '', '', '')