您现在的位置是:网站首页> 编程资料编程资料
asp.net使用H5新特性实现异步上传的示例_实用技巧_
2023-05-24
304人已围观
简介 asp.net使用H5新特性实现异步上传的示例_实用技巧_
###index.html
###index.js
function formDataUpload() { //这里可以一次性选中多个文件 var fileUpload = document.getElementById("FileToUpload").files; if (fileUpload.length == 0) { alert("请选中文件再上传"); return; } //html5新特性 var formdata = new FormData(); //添加上传数据 for (var i = 0; i < fileUpload.length;i++){ formdata.append('files', fileUpload[i]); } //使用javascript的原生ajax var xmlHttp = new XMLHttpRequest(); xmlHttp.open("post", 'Handler.ashx?method=formDataUpload'); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { alert("上传成功"); } } xmlHttp.send(formdata); } ###handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { formDataUpload(context); } public static void formDataUpload(HttpContext context) { //获取到客户端提交的文件 HttpFileCollection files = context.Request.Files; string msg = string.Empty; string error = string.Empty; int fileM = 0; if (files.Count > 0) { for (int i = 0; i < files.Count; i++) { ; String path = @"D:\"+files[i].FileName; files[i].SaveAs(path); fileM += files[i].ContentLength; } msg = "上传成功,文件总大小:" + fileM; string res = "{error :'" + error + "',msg:'" + msg + "'}"; context.Response.Write(res); context.Response.End(); } } public bool IsReusable { get { return false; } } } 以上这篇asp.net使用H5新特性实现异步上传的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
相关内容
- http转https的实战记录(iis 7.5)_实用技巧_
- .NET Core中依赖注入AutoMapper的方法示例_实用技巧_
- .NET连接数据库以及基本的增删改查操作教程_实用技巧_
- .Net整合Json实现REST服务客户端的方法详解_实用技巧_
- Visual Studio 2013+OpenCV2.4.10环境搭建教程_实用技巧_
- 利用Service Fabric承载eShop On Containers的实现方法_实用技巧_
- ASP.NET 谨用 async/await_实用技巧_
- 浅谈ASP.NET MVC 防止跨站请求伪造(CSRF)攻击的实现方法_实用技巧_
- ASP.NET Core文件上传与下载实例(多种上传方式)_实用技巧_
- ASP.NET MVC学习之NuGet在VS中的运用浅谈_实用技巧_
