在新增數據項的時候,用ajax實現無刷新提交,但上傳文件的時候,由於數據類型原因,不能將頁面的<asp:FileUpload>中以字符串值的方式傳到js裡調用。我一共找到了兩個方法予以解決,實現無刷新上傳。
第一種方法:利用js的ADODB.Stream,將文件先轉換成流,再通過js上傳到服務器,這樣有個好處就是可以上傳超大文件,並且由於是數據流,可以支持斷點續傳、方便顯示上傳進度等人性化功能。唯一的缺點是要客戶端浏覽器需要設置安全級別,或者安裝相關ActiveX控件(這個控件自己做的,加載到頁面中)。
相關代碼:
文件有:1個前台頁面:upload.html、 1個js控制:upload.js、 1個後台處理頁面:Accept.aspx(Accept.aspx.cs)
代碼文件如下:
upload.html
復制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</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) + '時';
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個參數表示上傳的類型,為命名新文件提供參考
//第2個參數表示要不要顯示狀態條
function BeginUpLoadFile(upFileType, guageFlag)
{
if(g_Stream == null)
{alert("您的機器不支持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 = "文件格式不正確,請選擇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);
}
}
}
當前1/2頁 12下一頁閱讀全文