jquery 批量上传图片实现代码

只限于IE,火狐下不能使用,因为火狐下得不到本地上传的图片路径

前台: upload.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>upload</title>
<link href="upload.css" rel="Stylesheet" />
</head>
<body>
<form>
<ul>
<li>
<button id="SubUpload" οnclick="TSubmitUploadImageFile();return false;">
确定上传</button>
<button id="CancelUpload" class="ManagerButton" οnclick="javascript:history.go(-1);">
取消</button>
<button id="AddUpload" class="ManagerButton" οnclick="TAddFileUpload();return false;">
增加</button>
</li>
</ul>
<ul id="loadimage">
<li>
<div class="imagelabel">
图片1:</div>
<div class="imagepath">
<input name="" size="45" id="uploadImg1" type="file" /></div>
<div class="loadinfo">
<span id="uploadImgState1"></span>
</div>
</li>
</ul>
</form>
</body>
</html>
<script type="text/javascript" src="http://www.cnblogs.com/JS/jquery-1.3.2-vsdoc.js"></script>
<script type="text/javascript">
var TfileUploadNum = 1; //记录图片选择框个数
var Tnum = 1; //ajax上传图片时索引
//增加上传按钮
function TAddFileUpload() {
var idnum = TfileUploadNum + 1;
var str = "<li>";
str += "<div class='imagelabel'>图片" + idnum + ":</div>";
str += "<div class='imagepath'><input name='' size='45' id='uploadImg" + idnum + "' type='file' /></div>";
str += "<div class='loadinfo'><span id='uploadImgState" + idnum + "'></span></div>";
str += "</li>";
$("#loadimage").append(str);
TfileUploadNum += 1;
}
//开始上传
function TSubmitUploadImageFile() {
document.getElementById("SubUpload").disabled = true;
document.getElementById("CancelUpload").disabled = true;
document.getElementById("AddUpload").disabled = true;
setTimeout("TajaxFileUpload()", 1000); //此为关键代码
}
//Ajax上传方法
function TajaxFileUpload() {
if (Tnum < TfileUploadNum + 1) {
//准备提交处理
$("#uploadImgState" + Tnum).html("<img src='/gif/upfileloader.gif'/>");
//开始提交
$.ajax({
type: "POST",
url: "Handler.ashx",
data: { upfile: $("#uploadImg" + Tnum).val()},
success: function(data, status) {
var stringArray = data.split("|");
//stringArray[0]    成功状态(1为成功,0为失败)
//stringArray[1]    上传成功的文件名
//stringArray[2]    消息提示
if (stringArray[0] == "1") {
//上传成功
$("#uploadImgState" + Tnum).html("<img src='/gif/Success.gif' />" + stringArray[1] + "--" + stringArray[2]);
}
else {
//上传出错
$("#uploadImgState" + Tnum).html("<img src='/gif/Error.gif' />" + stringArray[1] + "--" + stringArray[2]);
}
Tnum++;
setTimeout("TajaxFileUpload()", 1000);
}
});
}
else {
document.getElementById("SubUpload").disabled = false;
document.getElementById("CancelUpload").disabled = false;
document.getElementById("AddUpload").disabled = false;
}
}        
</script>

处理程序Handler.ashx


<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.IO;
using System.Text;
using System.Net;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//源图片路径
string _fileNamePath = "";
try
{
_fileNamePath = context.Request.Form["upfile"];
string _savedFileResult = UpLoadFile(_fileNamePath); //开始上传
context.Response.Write(_savedFileResult);//返回上传结果
}
catch
{
context.Response.Write("0|error|文件路径错误");
}
}
/// <summary>
/// 保存图片
/// </summary>
/// <param name="fileNamePath"></param>
/// <returns></returns>
private string UpLoadFile(string fileNamePath)
{
//图片格式
string fileNameExt = fileNamePath.Substring(fileNamePath.IndexOf('.')).ToLower();
if (!CheckFileExt(fileNameExt)) return "0|error|图片格式不正确!";
//保存路径
string toFilePath = "ProductUpload/";
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(toFilePath);
//检查是否有该路径 没有就创建
if (!Directory.Exists(toFileFullPath))
{
Directory.CreateDirectory(toFileFullPath);
}
//生成将要保存的随机文件名
string toFileName = GetFileName();
//将要保存的完整路径
string saveFile=toFileFullPath +toFileName + fileNameExt;
///创建WebClient实例
WebClient myWebClient = new WebClient();
//设定windows网络安全认证
myWebClient.Credentials = CredentialCache.DefaultCredentials;
//要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);      
BinaryReader r = new BinaryReader(fs);
//使用UploadFile方法可以用下面的格式
myWebClient.UploadFile(saveFile,fileNamePath);
return "1|"+toFileName+fileNameExt+"|保存成功.";
}
/// <summary>
/// 检测图片类型
/// </summary>
/// <param name="_fileExt"></param>
/// <returns>正确返回True</returns>
private bool CheckFileExt(string _fileExt)
{
string[] allowExt = new string[] { ".gif", ".jpg", ".jpeg" };
for (int i = 0; i < allowExt.Length; i++)
{
if (allowExt[i] == _fileExt) { return true; }
}
return false;
}
/// <summary>
/// 得到随机图片名
/// </summary>
/// <returns></returns>
public static string GetFileName()
{
Random rd = new Random();
StringBuilder serial = new StringBuilder();
serial.Append(DateTime.Now.ToString("yyMMddHHmmssff"));
serial.Append(rd.Next(0, 9999).ToString());
return serial.ToString();
}
public bool IsReusable
{
get
{
return false;
}
}
}

CSS样式 upload.css

body{font-size: 12pt;}
ul{list-style: none;}
li{margin: 0px;}
#loadimage{width: 860px;overflow: hidden;}
.imagelabel{ float: left; width: 60px; height: 25px;}
.imagepath{float: left; width: 400px; height: 25px; }
.loadinfo{float: left; width: 400px;height: 25px;}

在线演示:http://www.ncmem.com/products/image-uploader/demo/index.html 开发文档-ASP.NET(C#):http://www.cnblogs.com/xproer/archive/2011/01/09/1931278.html 开发文档-PHP:http://www.cnblogs.com/xproer/archive/2011/05/13/2045854.html 开发文档-JSP:http://www.cnblogs.com/xproer/archive/2011/05/20/2051887.html 产品介绍:http://www.cnblogs.com/xproer/archive/2010/08/09/1796077.html 升级日志:http://www.cnblogs.com/xproer/archive/2010/10/06/1844816.html 资源下载:crx安装包,xpi安装包,exe安装包,开发文档,ASP示例,ASP.NET示例,JSP示例,PHP示例, VC运行库:http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf 新颖网络图片上传插件是一款简化图片上传操作的插件。它提供了一些灵活的配置,能够帮助用户快速搭建起一个强大的图片分享平台。通过这些配置,用户还可以非常方便的同时上传多张图片,或将图片以指定的格式上传,免去手动转换图片格式的烦恼。 在最新版的图片上传控件中采用了全新的网络数据传输模块,新的模块全面优化了网络层的数据处理代码,同时在接收服务器返回的数据代码中采用精确识别的方式使数据处理效率更高。这些改进使图片上传控件具有了闪电般的上传速度。现在新颖网络图片上传控件在上传图片时平均每张图片帮助用户节省了50%的时间。 相信新颖网络图片上传控件能够为您的应用带来更好的用户体验。 产品特点如下: 1. 基于标准HTTP协议。 2. 支持BMP,JPG,GIF,PNG图片格式。 3. 支持自动生成缩略图。 4. 支持文件批量上传。 5. 支持文件拖拽操作。 6. 支持自定义上传信息。 7. 快速编辑。旋转操作。 8. 显示上传进度。 9. 支持文件格式批量转换。 10. 支持打开默认文件夹功能。 11. 免费提供JavaScript SDK包,方便您将插件快速集成到已有网站中。 支持语言:PHP,JSP,ASP,ASP.NET(C#),ASP.NET(VB),C++,VC,VC.NET,VB,VB.NET,C#,C#.NET,Delphi,C++Builder 支持平台:Visual Studio 6.0/2002/2003/2005/2008/2010,C++ Builder 6.0/2009/2010,Delphi 7/2009,Visual Basic 6.0/2008 支持脚本:JavaScript,VBScript 支持系统:Windows NT,Windows 2003,Windows XP,Windows Vista,Windows 7,Linux 支持浏览器:IE6,IE7,IE8,IE8(x64),IE9(x64),Firefox,Chrome,360安全浏览器,360极速浏览器,Maxthon1.x,Maxthon2.x,Maxthon3.x,QQ浏览器 支持图片格式:BMP,GIF,JPG,PNG,TIF
在线演示:http://www.ncmem.com/products/image-uploader/demo/index.html 开发文档-ASP.NET(C#):http://www.cnblogs.com/xproer/archive/2011/01/09/1931278.html 开发文档-PHP:http://www.cnblogs.com/xproer/archive/2011/05/13/2045854.html 开发文档-JSP:http://www.cnblogs.com/xproer/archive/2011/05/20/2051887.html 产品介绍:http://www.cnblogs.com/xproer/archive/2010/08/09/1796077.html 升级日志:http://www.cnblogs.com/xproer/archive/2010/10/06/1844816.html 资源下载:crx安装包,xpi安装包,exe安装包,开发文档,ASP示例,ASP.NET示例,JSP示例,PHP示例, VC运行库:http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf 新颖网络图片上传插件是一款简化图片上传操作的插件。它提供了一些灵活的配置,能够帮助用户快速搭建起一个强大的图片分享平台。通过这些配置,用户还可以非常方便的同时上传多张图片,或将图片以指定的格式上传,免去手动转换图片格式的烦恼。 在最新版的图片上传控件中采用了全新的网络数据传输模块,新的模块全面优化了网络层的数据处理代码,同时在接收服务器返回的数据代码中采用精确识别的方式使数据处理效率更高。这些改进使图片上传控件具有了闪电般的上传速度。现在新颖网络图片上传控件在上传图片时平均每张图片帮助用户节省了50%的时间。 相信新颖网络图片上传控件能够为您的应用带来更好的用户体验。 产品特点如下: 1. 基于标准HTTP协议。 2. 支持BMP,JPG,GIF,PNG图片格式。 3. 支持自动生成缩略图。 4. 支持文件批量上传。 5. 支持文件拖拽操作。 6. 支持自定义上传信息。 7. 快速编辑。旋转操作。 8. 显示上传进度。 9. 支持文件格式批量转换。 10. 支持打开默认文件夹功能。 11. 免费提供JavaScript SDK包,方便您将插件快速集成到已有网站中。 支持语言:PHP,JSP,ASP,ASP.NET(C#),ASP.NET(VB),C++,VC,VC.NET,VB,VB.NET,C#,C#.NET,Delphi,C++Builder 支持平台:Visual Studio 6.0/2002/2003/2005/2008/2010,C++ Builder 6.0/2009/2010,Delphi 7/2009,Visual Basic 6.0/2008 支持脚本:JavaScript,VBScript 支持系统:Windows NT,Windows 2003,Windows XP,Windows Vista,Windows 7,Linux 支持浏览器:IE6,IE7,IE8,IE8(x64),IE9(x64),Firefox,Chrome,360安全浏览器,360极速浏览器,Maxthon1.x,Maxthon2.x,Maxthon3.x,QQ浏览器 支持图片格式:BMP,GIF,JPG,PNG,TIF
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值