asp.net javascript 文件無刷新上傳實(shí)例代碼第1/2頁
在新增數(shù)據(jù)項(xiàng)的時(shí)候,用ajax實(shí)現(xiàn)無刷新提交,但上傳文件的時(shí)候,由于數(shù)據(jù)類型原因,不能將頁面的<asp:FileUpload>中以字符串值的方式傳到j(luò)s里調(diào)用。我一共找到了兩個(gè)方法予以解決,實(shí)現(xiàn)無刷新上傳。
第一種方法:利用js的ADODB.Stream,將文件先轉(zhuǎn)換成流,再通過js上傳到服務(wù)器,這樣有個(gè)好處就是可以上傳超大文件,并且由于是數(shù)據(jù)流,可以支持?jǐn)帱c(diǎn)續(xù)傳、方便顯示上傳進(jìn)度等人性化功能。唯一的缺點(diǎn)是要客戶端瀏覽器需要設(shè)置安全級(jí)別,或者安裝相關(guān)ActiveX控件(這個(gè)控件自己做的,加載到頁面中)。
相關(guān)代碼:
文件有:1個(gè)前臺(tái)頁面:upload.html、 1個(gè)js控制:upload.js、 1個(gè)后臺(tái)處理頁面:Accept.aspx(Accept.aspx.cs)
代碼文件如下:
upload.html
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標(biāo)題頁</title>
<script src="upload.js" src="upload.js" language="jscript" type="text/jscript"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="width:100%">
<input type="file" id="hidFilePath" />
<input type="button" id="ok" onclick="BeginUpLoadFile('0', true)" title="上傳" value="UpLoad"/>
</div>
<div id="lblLeavingsTime">TIME</div>
<div id="returnInfo">Info</div>
</form>
</body>
</html>
upload.js
var g_XMLHttp = null;
var g_Stream = new ActiveXObject('ADODB.Stream');
var g_SendCount = 0;
var g_TotalCount = 0;
var g_FileSize = 0;
var g_UpFileType = 0 ;
var g_BlockSize = 1024 * 100; //每段分為100K
var fileExtName = "" ; //文件后綴名
var g_PauseFlag = false;
var g_BeginTime = null;
var g_guageFlag = false ;
var Nfilename = "" ;
function Init()
{
InitTitleEvent();
BeginUpLoadFile();
}
function MoveGuage()
{
var t_Time = new Date();
var t_OddTime = Math.ceil((t_Time.getTime() - g_BeginTime.getTime()) / g_SendCount * (g_TotalCount - g_SendCount) / 1000);
var t_OddTimeString = '';
if(t_OddTime >= 3600)
{
t_OddTimeString = Math.floor(t_OddTime / 3600) + '時(shí)';
t_OddTime %= 3600;
}
if(t_OddTime >= 60)
{
t_OddTimeString += Math.floor(t_OddTime / 60) + '分';
t_OddTime %= 60;
}
document.all.lblLeavingsTime.innerText = t_OddTimeString + t_OddTime + '秒';
}
//第1個(gè)參數(shù)表示上傳的類型,為命名新文件提供參考
//第2個(gè)參數(shù)表示要不要顯示狀態(tài)條
function BeginUpLoadFile(upFileType, guageFlag)
{
if(g_Stream == null)
{alert("您的機(jī)器不支持ADODB.Stream."); }
else
{
g_guageFlag = guageFlag ;
g_UpFileType = upFileType;
g_Stream.Type = 1;
g_Stream.Open();
var pth = document.getElementById("hidFilePath").value ;
g_Stream.LoadFromFile(pth);
var fname=pth.split('\\');
Nfilename = fname[fname.length-1] ;
fileExtName = Nfilename.split('.')[1].toLowerCase();
g_Stream.position = 0;
g_SendCount = 1;
g_FileSize = g_Stream.size ;
if (upFileType == 0) //上傳圖片
{
if (g_FileSize > 1024 * 1024 * 2 ) // 不能大于2M
{
document.all.returnInfo.innerText = "文件大小超過2M!" ;
g_PauseFlag = true;
return ;
}
var str = "bmp,jpg,jpeg,gif,png,icon";
if (str.search(fileExtName) == -1) //圖片格式不能超出范圍
{
document.all.returnInfo.innerText = "文件格式不正確,請(qǐng)選擇bmp、jpg、jpeg、gif、png、icon格式圖片!" ;
g_PauseFlag = true;
return ;
}
}
g_TotalCount = Math.ceil(g_Stream.size / g_BlockSize);
g_BeginTime = new Date();
SendData();
}
}
function SendData()
{
if(g_PauseFlag)
{
return;
}
if(g_SendCount <= g_TotalCount)
{
var t_XMLDOM = null;
var t_Root = null;
var t_Node = null;
var t_Attribute = null;
t_XMLDOM = new ActiveXObject('Microsoft.XMLDOM');
t_XMLDOM.async = false;
t_XMLDOM.resolveExternals = false;
t_Node = t_XMLDOM.createProcessingInstruction('xml','version="1.0"');
t_XMLDOM.appendChild(t_Node);
t_Root = t_XMLDOM.createElement('Root');
t_XMLDOM.appendChild(t_Root);
t_XMLDOM.documentElement.setAttribute('xmlns:dt','urn:schemas-microsoft-com:datatypes');
t_Node = t_XMLDOM.createElement('Data');
t_Node.dataType = 'bin.base64';
t_Node.nodeTypedValue = g_Stream.Read(g_BlockSize);
t_Attribute = t_XMLDOM.createAttribute('upfiletype');
t_Attribute.value = g_UpFileType;
t_Node.setAttributeNode(t_Attribute);
t_Attribute = t_XMLDOM.createAttribute('fileindex');
t_Attribute.value = g_SendCount;
t_Node.setAttributeNode(t_Attribute);
t_Attribute = t_XMLDOM.createAttribute('totalcount');
t_Attribute.value = g_TotalCount;
t_Node.setAttributeNode(t_Attribute);
t_Attribute = t_XMLDOM.createAttribute('filesize');
t_Attribute.value = g_FileSize;
t_Node.setAttributeNode(t_Attribute);
t_Attribute = t_XMLDOM.createAttribute('blocksize');
t_Attribute.value = g_BlockSize;
t_Node.setAttributeNode(t_Attribute);
t_Attribute = t_XMLDOM.createAttribute('fileextname');
t_Attribute.value = fileExtName;
t_Node.setAttributeNode(t_Attribute);
t_Root.appendChild(t_Node);
g_XMLHttp = new ActiveXObject('Microsoft.XMLHttp');
g_XMLHttp.open('POST','AcceptFile.aspx',true);
g_XMLHttp.onreadystatechange = XMLHttpStateChange;
g_XMLHttp.send(t_XMLDOM);
if (g_guageFlag){ MoveGuage(); }
}
else
{
var xx = spider.BookFile.FileObj.getFileName() ;
alert(xx.value) ;
document.all.lblLeavingsTime.innerText = '0秒';
CloseWindow(document.all.cmdClose);
document.all.returnInfo.innerText = '文件上傳完成!';
}
}
function XMLHttpStateChange()
{
if(g_XMLHttp.readyState == 4)
{
var rstr = g_XMLHttp.responseText ;
if(rstr == 'OK')
{
g_SendCount++;
SendData();
}
else
{
document.all.returnInfo.innerText = rstr;
CloseWindow(document.all.cmdClose);
}
}
}
function CloseWindow(p_OBJ)
{
g_PauseFlag = true;
g_Stream.Close();
}
Accept.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AcceptFile.aspx.cs" Inherits="commonJS_AcceptFile" %>
Accept.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
using spider.BookFile; //這是自己寫的文件類
public partial class commonJS_AcceptFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(FileObj));
XmlDocument t_XmlDocument = new XmlDocument();
t_XmlDocument.Load(this.Request.InputStream);
XmlNode t_XmlNode = t_XmlDocument.SelectSingleNode("Root/Data");
FileObj t_FileOBJ = new FileObj();
string t_upfiletype = t_XmlNode.Attributes["upfiletype"].Value;
string t_FileIndex = t_XmlNode.Attributes["fileindex"].Value;
string t_totalcount = t_XmlNode.Attributes["totalcount"].Value;
string t_filesize = t_XmlNode.Attributes["filesize"].Value;
string t_blocksize = t_XmlNode.Attributes["blocksize"].Value;
string t_fileextname = t_XmlNode.Attributes["fileextname"].Value;
byte[] t_File = Convert.FromBase64String(t_XmlNode.FirstChild.Value);
FileObj.upfile myUpFile = new FileObj.upfile();
myUpFile.FileCount = t_totalcount;
myUpFile.FileIndex = t_FileIndex;
myUpFile.UpFileType = t_upfiletype;
myUpFile.FileSize = t_filesize;
myUpFile.BlockSize = t_blocksize;
myUpFile.ExtName = t_fileextname;
myUpFile.t_File = t_File;
FileObj.InsertFileList(myUpFile);
if (FileObj.getErrMsg == "")
{
this.Response.Write("OK");
}
else
{
this.Response.Write(FileObj.getErrMsg);
}
}
}
- nodejs利用ajax實(shí)現(xiàn)網(wǎng)頁無刷新上傳圖片實(shí)例代碼
- PHP+JavaScript實(shí)現(xiàn)無刷新上傳圖片
- SpringMVC結(jié)合ajaxfileupload.js實(shí)現(xiàn)文件無刷新上傳
- jsp+ajax實(shí)現(xiàn)無刷新上傳文件的方法
- js實(shí)現(xiàn)頭像圖片切割縮放及無刷新上傳圖片的方法
- js動(dòng)態(tài)創(chuàng)建上傳表單通過iframe模擬Ajax實(shí)現(xiàn)無刷新
- asp.net+js 實(shí)現(xiàn)無刷新上傳解析csv文件的代碼
- javascript仿163網(wǎng)盤無刷新文件上傳系統(tǒng)
- JavaScript實(shí)現(xiàn)無刷新上傳預(yù)覽圖片功能
相關(guān)文章
一個(gè)ASP.NET的MYSQL的數(shù)據(jù)庫操作類自己封裝的
這篇文章主要介紹了一個(gè)ASP.NET的MYSQL的數(shù)據(jù)庫操作類自己封裝的,在數(shù)據(jù)庫操作類中的連接字符串中記得加上charset=utf8 需要的朋友可以參考下2014-08-08.net采用ajax實(shí)現(xiàn)郵箱注冊(cè)和地區(qū)選擇實(shí)例
這篇文章主要介紹了.net采用ajax實(shí)現(xiàn)郵箱注冊(cè)和地區(qū)選擇的方法,以實(shí)例形式詳細(xì)講述了.net采用ajax的技巧,非常實(shí)用,需要的朋友可以參考下2014-10-10國產(chǎn)化中的?.NET?Core?操作達(dá)夢(mèng)數(shù)據(jù)庫DM8的兩種方式(操作詳解)
這篇文章主要介紹了國產(chǎn)化之?.NET?Core?操作達(dá)夢(mèng)數(shù)據(jù)庫DM8的兩種方式,這里提供兩種方式是傳統(tǒng)的DbHelperSQL方式和Dapper?方式,每種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04asp.net 簡易生成注冊(cè)碼(數(shù)字+大小寫字母)
注釋寫的很詳細(xì),不做過多的描述了,希望能給初學(xué)者帶來一些幫助,同時(shí)也是自己知識(shí)的一個(gè)積累過程。2008-11-11Visual Studio 2017開發(fā)環(huán)境的安裝圖文教程
Visual Studio 2017是微軟于2017年3月8日正式推出的新版本,是迄今為止 最具生產(chǎn)力 的 Visual Studio 版本。這篇文章主要介紹了Visual Studio 2017開發(fā)環(huán)境的安裝,需要的朋友可以參考下2017-11-11asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法,涉及asp.net操作office文件的相關(guān)技巧,需要的朋友可以參考下2016-08-08asp.net代碼中修改web.config節(jié)點(diǎn)的具體方法
在有些情況下,要在代碼中讀取一種全局變量,把這種全局變量放在web.config是一種常見的手段。2013-06-06ASP.NET Core跨站登錄重定向的實(shí)現(xiàn)新姿勢(shì)
這篇文章主要給大家介紹了關(guān)于ASP.NET Core實(shí)現(xiàn)跨站登錄重定向的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07