欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-21 11:42:58
需要下载neatupload插件 上传页面: <%@ page language="c#" autoeventwireup="true" codefile...

需要下载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 href="easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
  <link 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 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 href="easyui/themes/default/easyui.css" rel="stylesheet" type="text/css" />
  <link 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 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 href="default2.aspx?1=<%=item%>">
        <%=item %></a>     <a 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 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 href="###" style="color:red;font-weight:700;" onclick="uppage(' + i + ')">' + (i + 1) + '</a> '
      else strc += '<a href="###" onclick="uppage(' + i + ')">' + (i + 1) + '</a> '
    } //欢迎来到站长特效网,我们的网址是www.zzjs.net,很好记,zz站长,js就是js特效,本站收集大量高质量js代码,还有许多广告代码下载。
    stre = ' <a 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");
}

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