ASP.NET实现大文件上传功能

春季来好快,悄无声息不知不觉中,草儿绿了,枝条发芽了,遍地野花油菜花开灿烂多姿,一切沐浴着春晨曙光,春风中摇弋轻摆,仿佛少女轻歌曼舞,楚楚动人。

需要下载NeatUpload插件

上传页面:

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

<%@ Register Assembly="Brettle.Web.NeatUpload" Namespace="Brettle.Web.NeatUpload"
  TagPrefix="Upload" %>

<!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>
   <link rel="nofollow noopener noreferrer" href="easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
  <link rel="nofollow noopener noreferrer" href="easyui/themes/icon.css" rel="stylesheet" type="text/css" />
  <script src="easyui/jquery.min.js" type="text/javascript"></script>
  <script src="easyui/jquery.easyui.min.js" type="text/javascript"></script>
</head>
<body>
  <form id="form1" runat="server">
  <div class="easyui-panel" style="padding:5px;">
    <Upload:ProgressBar ID="ProgressBar1" runat='server'>
    </Upload:ProgressBar>
   <br />
  <Upload:InputFile ID="AttachFile" runat="server" />
  <br />
  <br />
  <asp:LinkButton ID="BtnUP" runat="server" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="BtnUP_Click">上 传</asp:LinkButton><br /><br />
  <a rel="nofollow noopener noreferrer" href="Down.aspx">查看下载</a>
  </div>
  </form>
</body>
</html>

后台源码:

protected void BtnUP_Click(object sender, EventArgs e)
  {
    if (AttachFile.HasFile)
    {
      string FileName = this.AttachFile.FileName;//获取上传文件的文件名,包括后缀
      string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
      string SaveFileName = System.IO.Path.Combine(System.Web.HttpContext.Current.Request.MapPath("UpLoads/"), DateTime.Now.ToLongDateString().ToString() + "-" + FileName);//合并两个路径为上传到服务器上的全路径
      AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
      string url = "UpLoads/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ExtenName; //文件保存的路径
      float FileSize = (float)System.Math.Round((float)AttachFile.ContentLength / 1024000, 1); //获取文件大小并保留小数点后一位,单位是M
      Response.Write("<script>alert('恭喜您,上传成功!')</script>");
    }
    else
    {
      Response.Write("<script>alert('请选择文件!')</script>");
    }
  }

这里直接获取上传文件夹文件名称提供下载

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

<!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>
  <link rel="nofollow noopener noreferrer" href="easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
  <link rel="nofollow noopener noreferrer" href="easyui/themes/icon.css" rel="stylesheet" type="text/css" />
  <script src="easyui/jquery.min.js" type="text/javascript"></script>
  <script src="easyui/jquery.easyui.min.js" type="text/javascript"></script>
  <style type="text/css">
    .div_style ul li
    {
       list-style-type: decimal;
       border-bottom:1px dashed #E3E3E3;
       margin-bottom:5px;
    }
    .div_style ul li a
    {
      text-decoration: none;
      cursor: pointer;
      font-size: 16px;
    }
  </style>
</head>
<body>
  <form id="form1" runat="server">
  <div class="div_style easyui-panel" style="padding: 5px;">
  <a rel="nofollow noopener noreferrer" href="index.aspx">上传文件</a>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <ul id="www_zzjs_net">
      <%if (files.Length == 0) { Label1.Text = "暂无文件"; }
       else
       {

         foreach (System.IO.FileInfo item in files)
         {%>
      <li><a rel="nofollow noopener noreferrer" href="Default2.aspx?1=<%=item%>">
        <%=item %></a>  <a rel="nofollow noopener noreferrer" href="delete.aspx?1=<%=item%>" onclick="javascript:return confirm('确定删除吗?');" >删除附件</a>
      </li>
      <% }
     }%>
    </ul>
  </div>
  <div id="changpage"></div>
  <%--js分页--%>
<%--<script language="javascript">
  var obj, j;
  var page = 0;
  var nowPage = 0; //当前页
  var listNum = 25; //每页显示<ul>数
  var PagesLen; //总页数
  var PageNum = 4; //分页链接接数(5个)
  onload = function () {
    obj = document.getElementById("www_zzjs_net").getElementsByTagName("li");
    j = obj.length
    PagesLen = Math.ceil(j / listNum);
    upPage(0)
  }
  function upPage(p) {
    nowPage = p
    //内容变换
    for (var i = 0; i < j; i++) {
      obj[i].style.display = "none"
    }
    for (var i = p * listNum; i < (p + 1) * listNum; i++) {
      if (obj[i]) obj[i].style.display = "block"
    } //欢迎来到站长特效网,我们的网址是www.zzjs.net,很好记,zz站长,js就是js特效,本站收集大量高质量js代码,还有许多广告代码下载。
    //分页链接变换
    strS = '<a rel="nofollow noopener noreferrer" href="###" onclick="upPage(0)">首页</a> '
    var PageNum_2 = PageNum % 2 == 0 ? Math.ceil(PageNum / 2) + 1 : Math.ceil(PageNum / 2)
    var PageNum_3 = PageNum % 2 == 0 ? Math.ceil(PageNum / 2) : Math.ceil(PageNum / 2) + 1
    var strC = "", startPage, endPage;
    if (PageNum >= PagesLen) { startPage = 0; endPage = PagesLen - 1 }
    else if (nowPage < PageNum_2) { startPage = 0; endPage = PagesLen - 1 > PageNum ? PageNum : PagesLen - 1 } //首页
    else { startPage = nowPage + PageNum_3 >= PagesLen ? PagesLen - PageNum - 1 : nowPage - PageNum_2 + 1; var t = startPage + PageNum; endPage = t > PagesLen ? PagesLen - 1 : t }
    for (var i = startPage; i <= endPage; i++) {
      if (i == nowPage) strC += '<a rel="nofollow noopener noreferrer" href="###" style="color:red;font-weight:700;" onclick="upPage(' + i + ')">' + (i + 1) + '</a> '
      else strC += '<a rel="nofollow noopener noreferrer" href="###" onclick="upPage(' + i + ')">' + (i + 1) + '</a> '
    } //欢迎来到站长特效网,我们的网址是www.zzjs.net,很好记,zz站长,js就是js特效,本站收集大量高质量js代码,还有许多广告代码下载。
    strE = ' <a rel="nofollow noopener noreferrer" href="###" onclick="upPage(' + (PagesLen - 1) + ')">尾页</a> '
    strE2 = nowPage + 1 + "/" + PagesLen + "页" + " 共" + j + "条"
    document.getElementById("changpage").innerHTML = strS + strC + strE + strE2
  }
</script>--%>
  </form>
</body>
</html>

//下面是后台代码
public FileInfo[] files { set; get; }
protected void Page_Load(object sender, EventArgs e)
{
string dirPath = HttpContext.Current.Server.MapPath("~/UpLoads");
if (Directory.Exists(dirPath))
{
DirectoryInfo dir = new DirectoryInfo(dirPath);
files = dir.GetFiles("*.*");
}
if (files.Length == 0) { Label1.Text = "暂无文件"; }
}

 

//这是新建另一个页面提供下载的代码

if (Request.QueryString["1"] != null)
{


string _filename = Request.QueryString["1"].ToString(), _filepath = System.IO.Path.Combine(MapPath("~/UpLoads"), _filename);
if (System.IO.File.Exists(_filepath))
{
Response.Clear();
Response.Buffer = true;

Response.AddHeader("Content-Disposition", "attachment;filename=" + _filename);
Response.ContentType = "application/unknow";
Response.TransmitFile(_filepath);
Response.End();
}
}

 

//这是新建另一个页面提供删除操作(后台)

string _filename = Request.QueryString["1"].ToString(), _filepath = System.IO.Path.Combine(MapPath("~/UpLoads"), _filename);
if (System.IO.File.Exists(_filepath))
{
File.Delete(_filepath);
Response.Write("<script language=javascript>alert('删除成功!');window.location = 'Down.aspx';</script>");
//Response.Redirect("Down.aspx");
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

您可能有感兴趣的文章
.NET Core Web APi大文件分片上传研究实现

NetCore 3.0文件上传和大文件上传的限制详解

ASP.NET对大文件上传的解决方案

ASP.Net下载大文件的实现方法

在ASP.NET中支持断点续传下载大文件(ZT)源码