您现在的位置是:网站首页> 编程资料编程资料

ASP.NET实现大文件上传功能_实用技巧_

2023-05-24 444人已围观

简介 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" %>

后台源码:

 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(""); } else { Response.Write(""); } }

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Down.aspx.cs" Inherits="Down" %>
上传文件
    <%if (files.Length == 0) { Label1.Text = "暂无文件"; } else { foreach (System.IO.FileInfo item in files) {%>
  • <%=item %>     删除附件
  • <% } }%>
<%--js分页--%><%----%>
//下面是后台代码 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(""); //Response.Redirect("Down.aspx"); }

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

-六神源码网