ASP.NET的簡單檔案上傳及下載程式

因為某些用途有需要用到這個簡單的小程式,因此特別把它記在這裡,以免日後有用到還要再重新寫一次。特別提醒下列注意事項:

  1. Response.WriteFile();只是一種簡單的下載解決方案,真正的大型網站在實務上不會使用這種方式來打檔案。
  2. Internet Explorer(IE)在檔名是中文字的模式時,會出現亂碼,這個問題有跨瀏覽器的解決方案,但這不是這篇文章的討論目的。
  3. 寫入目的端之目錄,必須加入「IIS_IUSRS」完全控制權限。

再次強調,這個程式碼只供測試,因為中間有很多初學者不知道的漏洞存在,請勿做正式用途否則後果不堪設想。程式碼如下方所列:

<%@ Page Language="C#" %>
<script runat="server">
  String savePath = @"D:\FileTemp";
  protected void UploadButton_Click(object sender, EventArgs e)
  {
    showFilePath.Text = savePath;    
    if (FileUpload1.HasFile)
    {
      FileUpload1.SaveAs(savePath + @"\" + FileUpload1.FileName);
      showFileName.Text = FileUpload1.FileName;
    }
    else
    {      
      showFileName.Text = "error";
    }
  }

  protected void dlFiles(object sender, EventArgs e)
  {
    if(showFileName.Text != "" && showFileName.Text != "error")
    {
      Response.ContentType = "application/octet-stream";
      Response.AddHeader("Content-Disposition", "attachment;filename=" + showFileName.Text);
      Response.WriteFile(savePath + @"\" + showFileName.Text);
    }
  }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>檔案上傳範例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <h4>透過網頁上傳檔案</h4>
       <asp:FileUpload id="FileUpload1" runat="server"/>
       <asp:Button Text="上傳檔案" OnClick="UploadButton_Click" runat="server"/>
       <hr />
       <p>檔名: <asp:Literal id="showFileName" runat="server" /></p>
       <p>路徑: <asp:Literal id="showFilePath" runat="server" /></p>
       <p><asp:button text="下載檔案" OnClick="dlFiles" runat="server"/></p>
    </div>
    </form>
</body>
</html>
ASP.NET FileUpload Response.WriteFile