当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net javascript 文件无刷新上传实例代码

ASP.NET
细细品味ASP.NET(一)
细细品味ASP.NET(二)
细细品味ASP.NET(三)
细细品味ASP.NET(四)
细细品味ASP.NET(五)
用asp.net和xml做的新闻更新系统(1)
用asp.net和xml做的新闻更新系统(2)
用asp.net和xml做的新闻更新系统(3)
十天学会ASP.net(1)
十天学会ASP.net(2)
十天学会ASP.net(3)
十天学会ASP.net(4)
十天学会ASP.net(5)
十天学会ASP.net(6)
十天学会ASP.net(7)
十天学会ASP.net(8)
十天学会ASP.net(9)
十天学会ASP.net(10)
ASP.NET创建XML Web服务全接触(1)
ASP.NET创建XML Web服务全接触(2)

ASP.NET 中的 asp.net javascript 文件无刷新上传实例代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 189 ::
收藏到网摘: n/a

最近在写C# .net代码的时候,遇到一个上传刷新的问题。

在新增数据项的时候,用ajax实现无刷新提交,但上传文件的时候,由于数据类型原因,不能将页面的<asp:FileUpload>中以字符串值的方式传到js里调用。我一共找到了两个方法予以解决,实现无刷新上传。
第一种方法:利用js的ADODB.Stream,将文件先转换成流,再通过js上传到服务器,这样有个好处就是可以上传超大文件,并且由于是数据流,可以支持断点续传、方便显示上传进度等人性化功能。唯一的缺点是要客户端浏览器需要设置安全级别,或者安装相关ActiveX控件(这个控件自己做的,加载到页面中)。
相关代码:
文件有:1个前台页面:upload.html、 1个js控制:/upload/tech/20091011/20091011144728_ef0d3930a7b6c95bd2b32ed45989c61f.js、 1个后台处理页面:Accept.aspx(Accept.aspx.cs)
代码文件如下:
upload.html

复制代码 代码如下:
第二种方法:用js动态创建form和iframe上传文件,实现无刷新。优点是代码量小,无需客户端安装控件,缺点就是上传有限制大小,下面看代码:
需要文件有:1个前台页面upload.html、 1个js函数 function upFile、 1个处理文件accept.aspx(accept.aspx.cs)
upload.html
复制代码 代码如下:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script src="/upload/tech/20091011/20091011144728_ef0d3930a7b6c95bd2b32ed45989c61f.js" src="/upload/tech/20091011/20091011144728_ef0d3930a7b6c95bd2b32ed45989c61f.js" language="jscript" type="text/jscript"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="width:100%">
<iframe name='hidden_frame' id="hidden_frame" style='width:150px; height:50px; display:none;'> </iframe>
<input type="file" id="hidFilePath" />
<input id="upBtn" type="button" class="clearBtn" style="display:none;" value="上传图片" onclick="upFile('hidFilePath');" />
</div>
</form>
</body>
</html>

function upFile
复制代码 代码如下:

function upFile(ob)
{
var file = document.getElementById(ob) ;
var newName = "FileName"; //设置文件保存的名字
var form=document.createElement('form');
document.body.appendChild(form);
form.encoding="multipart/form-data";
form.method = "post";
form.action= "accept.aspx?nm=" + newName;
form.target= "hidden_frame";
var pos=file.nextSibling; //记住file在旧表单中的的位置
form.appendChild(file);
form.submit();
pos.parentNode.insertBefore(file,pos);
document.body.removeChild(form);
}

accept.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="up.aspx.cs" Inherits="Member_up" %>
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;

public partial class Member_up : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mz = HttpContext.Current.Request.QueryString["nm"].ToString();
string uperr = "";
HttpFileCollection files = HttpContext.Current.Request.Files;
if (files.Count>0)
{ uperr = upSingleFile(files[0], mz); }
else { uperr = "ok"; }
HttpContext.Current.Session["upInfo"] = uperr;
Response.Write(uperr);
}

//上传文件
private string upSingleFile(HttpPostedFile file, String theFileName)
{
string infos = "";
bool fileOK = false;
string fileName, fileExtension ;
fileName = System.IO.Path.GetFileName(file.FileName);
if (fileName != "")
{
if (file.ContentLength >= 1024 * 1024 * 2)
{
infos = "上传文件太大,目前仅支持2M以内的图片上传!";
}
else
{
fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
String[] allowedExtensions = { ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".icon" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
break;
}
}
if (!fileOK)
{
infos = "不支持上传此类型文件!目前支持的图片格式有:jpg|jpeg|gif|bmp|png|icon";
}
else
{
file.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/books/BookPic/") + theFileName);
infos = "ok" + theFileName;
}
}
}
else
{
infos = "没有读取到文件!";
}
return infos;
}
}

以上为我写程序过程中遇到过的问题和探索到的解决方法,写下来一是自己在温习巩固一遍,二来是为了与大家分享,请大家指正需改进之处,以期达到共同进步!