asp.net使用H5新特性實(shí)現(xiàn)異步上傳的示例
###index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="Script/jquery-1.10.2.min.js"></script>
<script src="Script/index.js"></script>
<title></title>
<script type="text/javascript">
$(function(){
$("#ajaxFileUpload").click(function () {
formDataUpload();
});
});
</script>
</head>
<body>
<input type="file" id="FileToUpload" multiple="multiple" mame="FileToUpload" />
<input type="button" id="ajaxFileUpload" value="上傳"/>
<input type="text" size="10"/>
</body>
</html>
###index.js
function formDataUpload() {
//這里可以一次性選中多個(gè)文件
var fileUpload = document.getElementById("FileToUpload").files;
if (fileUpload.length == 0) {
alert("請(qǐng)選中文件再上傳");
return;
}
//html5新特性
var formdata = new FormData();
//添加上傳數(shù)據(jù)
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新特性實(shí)現(xiàn)異步上傳的示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán)功能
這篇文章主要介紹了ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán),本文將分別介紹?Authentication(認(rèn)證)?和?Authorization(授權(quán)),通過實(shí)例代碼分別介紹了這兩個(gè)功能,需要的朋友可以參考下2022-04-04
asp.net ASPxTextBox等控件實(shí)現(xiàn)"回車模擬Tab"的 常用代碼整理
今天我要實(shí)現(xiàn)一些編輯框如ASPxTextBox、ASPxComboBox等控件回車模擬Tab的功能。這沒辦法,用戶用慣了回車,討厭按Tab來移動(dòng)焦點(diǎn)(鼠標(biāo)點(diǎn)擊更麻煩)。2010-03-03
如何使用ASP.NET創(chuàng)建網(wǎng)站并設(shè)計(jì)web頁面
這篇文章主要介紹了如何使用ASP.NET創(chuàng)建網(wǎng)站,幫助大家更好的理解和學(xué)習(xí)使用ASP.NET技術(shù),感興趣的朋友可以了解下2021-04-04
ASP.NET?MVC增加一條記錄同時(shí)添加N條集合屬性所對(duì)應(yīng)的個(gè)體
這篇文章介紹了ASP.NET?MVC增加一條記錄同時(shí)添加N條集合屬性所對(duì)應(yīng)個(gè)體的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
gridview和checkboxlist的嵌套相關(guān)應(yīng)用
gridview和checkboxlist的嵌套使用,會(huì)有效的提高開發(fā)的效率,不過很多的童鞋們對(duì)此還是很陌生的,接下來將幫助童鞋們實(shí)現(xiàn)gridview和checkboxlist的嵌套使用,感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性...2006-09-09
asp頁面和Asp.net頁面?zhèn)髦形膮?shù)UrlEncode編碼以及接收解碼
在asp中加一個(gè)鏈接,指向asp.net網(wǎng)頁,但asp.net的網(wǎng)址是經(jīng)過HttpUtility.UrlEncode轉(zhuǎn)換和HttpUtility.UrlDecode解碼的,而asp的server.urlencode卻和HttpUtility.UrlEncode的編碼方式不一樣.2010-04-04
asp.net sql 數(shù)據(jù)庫處理函數(shù)命令
asp.net sql 數(shù)據(jù)庫處理函數(shù)命令 ,需要的朋友可以參考下。2009-10-10

