拥有网页版小U盘 ASP.NET实现文件上传与下载功能

2019-05-22 10:37:31王旭

说完了上传,下面谈一谈文件的下载。这里主要是借助于Directory对象的GetFiles()方法,其可以获得指定路径下的所有的文件的名称。这样我们就可以用之来填充一个listBox,来供我们选择到底要下载那一个文件。
也许这时你会有一点疑惑了,我现在知道了有哪些文件可以下载,那下一步我要怎么来实现呢?
其实这里是利用了Session的存储机制,那就是将我们在listbox 中选择的item的内容记录到session的特定的key中,这样的话,我们就可以不用关心这些信息在页面间是怎么传输的了。只需要在想要进行下载的地方直接获取就可以了。
最为核心的是下载的过程:

if (filepathinfo.Exists)
      {
        //save the file to local
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filepathinfo.Name));
        Response.AddHeader("Content-length", filepathinfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.Filter.Close();
        Response.WriteFile(filepathinfo.FullName);
        Response.End();
      }


下面看一下,下载界面的布局文件吧

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DownFile.aspx.cs" Inherits="DownFile" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:ImageButton ID="ImageButton_Up" runat="server" Height="56px" OnClick="ImageButton_Up_Click" ToolTip="Upload" Width="90px" />
           
    <asp:ImageButton ID="ImageButton_Down" runat="server" Height="52px" OnClick="ImageButton_Down_Click" style="margin-top: 0px" ToolTip="Download" Width="107px" />
       
  <div>

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:ListBox ID="ListBox1" runat="server" Height="169px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" Width="371px"></asp:ListBox>

  </div>
  </form>
</body>
</html>

 然后是具体的逻辑代码实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class DownFile : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)//the first time to load
    {
      //get all the file in File folder
      String[] AllTxt = Directory.GetFiles(Server.MapPath("File"));
      foreach (String name in AllTxt)
      {
        ListBox1.Items.Add(Path.GetFileName(name));
      }
    }
  }
  protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
  {
    //make use of sssion to save the selected file in the listbox with the key of "select"
    Session["select"] = ListBox1.SelectedValue.ToString();
  }
  protected void ImageButton_Down_Click(object sender, ImageClickEventArgs e)
  {
    //judge weather user choose at least one file
    if (ListBox1.SelectedValue != "")
    {
      //get the path of the choosed file
      String FilePath = Server.MapPath("File/") + Session["select"].ToString();
      //initial the object of Class FileInfo and make it as the package path
      FileInfo filepathinfo = new FileInfo(FilePath);
      //judge weather the file exists
      if (filepathinfo.Exists)
      {
        //save the file to local
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filepathinfo.Name));
        Response.AddHeader("Content-length", filepathinfo.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.Filter.Close();
        Response.WriteFile(filepathinfo.FullName);
        Response.End();
      }
      else
      {
        Page.RegisterStartupScript("sb", "<script>alert('Please choose one file,sir!')</script>");
      }
    }
  }
  protected void ImageButton_Up_Click(object sender, ImageClickEventArgs e)
  {
    Response.Redirect("Default.aspx");
  }
}