这些天一直在做一个无刷新的文件上传功能,现在也总算是做好。可以说是是几经波折吧,开始一直想用AJAX的思路去实现,没想到费了很多时间最后还是徒劳!用ADODB.STREAM 应该也是可以把表单的文件POST到Httphandler中来处理,但是其中会遇到很多麻烦的事情,比如系统禁用了ADODB.DLL的,IE里面没权限访问数据等,总之很麻烦也不实用。
还是使用了将FORM提交到一个隐藏IFRAME中实现无刷新的上传文件。实现起来也非常容易,早知道的话我就直接用这种方法了,就不会浪费这么多时间。不过,我试了那么多方法也没白话时间吧,至少我知道了那样的方法是不行的!方法不是原创,很早就有人实现了,我是从网络收集的,这里贴出以作备忘及给有需要的人。下面是实现这一功能的关键代码:
HTML部分就是一个隐藏IFRAME,FORM以及INPUT
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> <!-- 隐藏的iframe来接受表单提交的信息 -->
< iframe. name = " hd " style = " display:none; " > iframe >
< FORM. id = " Form1 " method = " post " action = " upload.ashx " enctype = " multipart/form-data " target = " hd " >
< div id = " screenshot " style = " display:block; background-color:#FFFF99; position:absolute;width:350px; left:8px; bottom:137px;border:1px; border-color:Gray;border-style.:solid; z-index:1; " >
< input type = " file " id = " file1 " name = " upfile " size = " 28 " />
< input type = " button " value = " 发送 " onclick = " return checksn(); " />
div >
< FORM />
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> <!-- 隐藏的iframe来接受表单提交的信息 -->
< iframe. name = " hd " style = " display:none; " > iframe >
< FORM. id = " Form1 " method = " post " action = " upload.ashx " enctype = " multipart/form-data " target = " hd " >
< div id = " screenshot " style = " display:block; background-color:#FFFF99; position:absolute;width:350px; left:8px; bottom:137px;border:1px; border-color:Gray;border-style.:solid; z-index:1; " >
< input type = " file " id = " file1 " name = " upfile " size = " 28 " />
< input type = " button " value = " 发送 " onclick = " return checksn(); " />
div >
< FORM />
JAVASCRIPT部分:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> function UpdateMsg(newfilename, filename) { // 此函数用来提供给提交到的页面如upload.ashx输出js的回调,更新当前页面的信息
if (filename == '' ) { alert( ' 未上传文件! ' ); return false ; }
alert( " 上传成功 \n储存的文件名 " + newfilename + " \n原文件名 " + filename);
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> function UpdateMsg(newfilename, filename) { // 此函数用来提供给提交到的页面如upload.ashx输出js的回调,更新当前页面的信息
if (filename == '' ) { alert( ' 未上传文件! ' ); return false ; }
alert( " 上传成功 \n储存的文件名 " + newfilename + " \n原文件名 " + filename);
}
服务器端upload.ashx
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> @ WebHandler Language="C#" Class="upload" %>
using System;
using System.Web;
public class upload : IHttpHandler {
private string Js(string v) {//此函数进行js的转义替换的,防止字符串中输入了'后造成回调输出的js中字符串不闭合
if (v == null) return "";
return v.Replace("'", @"\'");
}
//下面就是一个简单的示例,保存上传的文件,如果要验证上传的后缀名,得自己写,还有写数据库什么的
public void ProcessRequest (HttpContext context) {
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
HttpServerUtility Server = context.Server;
//指定输出头和编码
Response.ContentType = "text/plain";
Response.Charset = "gb2313";
HttpPostedFile f = Request.Files["upfile"];//获取上传的文件
string oldfile = f.FileName.Substring(f.FileName.LastIndexOf("\\")+1);
int len = f.ContentLength;
string des = Request.Form["des"]//获取描述
,newFileName=Guid.NewGuid().ToString();//使用guid生成新文件名
if (len > 5 * 1024 * 1024)
{
Response.Write("");//只能上传5MB以内的文件
Response.End();
return;
}
if (f.FileName == "")//未上传文件
Response.Write("");//输出js,使用parent对象得到父页的引用
else { //保存文件
newFileName += System.IO.Path.GetExtension(f.FileName);//注意加上扩展名
try {
f.SaveAs(Server.MapPath("~/UploadFile/" + newFileName));//如果要保存到其他地方,注意修改这里
//调用父过程更新内容,注意要对des变量进行js转义替换,繁殖字符串不闭合提示错误
Response.Write("" + Js(newFileName) + "','" + oldfile + " " + len + "byte" + "')");
}
catch {
Response.Write("");//如果保存失败,输出js提示保存失败
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> @ WebHandler Language="C#" Class="upload" %>
using System;
using System.Web;
public class upload : IHttpHandler {
private string Js(string v) {//此函数进行js的转义替换的,防止字符串中输入了'后造成回调输出的js中字符串不闭合
if (v == null) return "";
return v.Replace("'", @"\'");
}
//下面就是一个简单的示例,保存上传的文件,如果要验证上传的后缀名,得自己写,还有写数据库什么的
public void ProcessRequest (HttpContext context) {
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
HttpServerUtility Server = context.Server;
//指定输出头和编码
Response.ContentType = "text/plain";
Response.Charset = "gb2313";
HttpPostedFile f = Request.Files["upfile"];//获取上传的文件
string oldfile = f.FileName.Substring(f.FileName.LastIndexOf("\\")+1);
int len = f.ContentLength;
string des = Request.Form["des"]//获取描述
,newFileName=Guid.NewGuid().ToString();//使用guid生成新文件名
if (len > 5 * 1024 * 1024)
{
Response.Write("");//只能上传5MB以内的文件
Response.End();
return;
}
if (f.FileName == "")//未上传文件
Response.Write("");//输出js,使用parent对象得到父页的引用
else { //保存文件
newFileName += System.IO.Path.GetExtension(f.FileName);//注意加上扩展名
try {
f.SaveAs(Server.MapPath("~/UploadFile/" + newFileName));//如果要保存到其他地方,注意修改这里
//调用父过程更新内容,注意要对des变量进行js转义替换,繁殖字符串不闭合提示错误
Response.Write("" + Js(newFileName) + "','" + oldfile + " " + len + "byte" + "')");
}
catch {
Response.Write("");//如果保存失败,输出js提示保存失败
}
}
}
public bool IsReusable {
get {
return false;
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-664108/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12639172/viewspace-664108/