在新增数据项的时候,用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>
<input type="file" id="hidFilePath" />
<input type="button" id="ok" 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下一页阅读全文
【asp.net javascript 文件无刷新上传实例代码第1/2页】相关文章:
★ asp.net下gridview 批量删除的实现方法第1/3页
★ asp.net neatUpload 支持大文件上传组件
★ asp.net datalist绑定数据后可以上移下移实现示例
★ asp.net Server.MapPath方法注意事项第1/2页
★ asp.net Web Services上传和下载文件(完整代码)第1/2页