时间:2021-07-01 10:21:17 帮助过:4人阅读
先上效果图:
具体代码如下:
在页面中如下
完整页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>文件批量上传Demo
UploadHandler.ashx代码:
using System; using System.Web; using System.IO; public class UploadHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Request.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); context.Response.Charset = "UTF-8"; if (context.Request.Files.Count > 0) { #region 获取上传路径 string uploadFolder = GetUploadFolder(); #endregion if (System.IO.Directory.Exists(uploadFolder)) {//如果上传路径存在 HttpPostedFile file = context.Request.Files["Filedata"]; string filePath = Path.Combine(uploadFolder, file.FileName); file.SaveAs(filePath); context.Response.Write("0"); } else { context.Response.Write("2"); } } } public bool IsReusable { get { return false; } } ////// 返回不带后缀的文件名 /// /// ///public static string GetFirstFileName(string fileName) { return Path.GetFileNameWithoutExtension(fileName); } /// /// 获取上传目录 /// ///public static string GetUploadFolder() { string rootPath = HttpContext.Current.Server.MapPath("~"); return Path.Combine(rootPath, "test"); } }
文件上传.NET默认有大小限制,像IIS限制的30M默认请求大小。如果不想修改IIS,又想突破这个大小的限制,比如上传1GB大小的文件。
这是修改Web.config即可实现。
<?xml version="1.0" encoding="utf-8"?>
希望本文所述对大家学习javascript程序设计有所帮助。