asp.net javascript 文件无刷新上传实例代码第1/2页

时间:2021-05-28

在新增数据项的时候,用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://monJS_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);
}
}
}

12下一页阅读全文

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章