您现在的位置是:网站首页> 编程资料编程资料
ASP.NET实现大文件上传功能_实用技巧_
2023-05-24
445人已围观
简介 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" %>//下面是后台代码 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"); }
以上就是本文的全部内容,希望对大家的学习有所帮助。
您可能感兴趣的文章:
相关内容
- 一句话轻松搞定asp.net分页_实用技巧_
- ASP.NET MVC5验证系列之Remote Validation_实用技巧_
- 解决VS2015中没有报表项(ReportViewer)的方法_实用技巧_
- asp.net mvc实现简单的实时消息推送_实用技巧_
- MVC+EasyUI+三层架构简单权限管理系统_实用技巧_
- ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容_实用技巧_
- 关于多对多关系表无法更新与插入的问题_实用技巧_
- asp.net TreeView递归循环子节点生成树形菜单实例_实用技巧_
- MVC默认路由实现分页(PagerExtend.dll下载)_实用技巧_
- WPF气泡样式弹窗效果代码分享_实用技巧_
