62,046
社区成员
发帖
与我相关
我的任务
分享
<!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>
<title>Web图片批量上传控件演示页面</title>
<script type="text/javascript" src="ImageUploader/ImageUploader.js" charset="utf-8"></script>
</head>
<body>
<div><a href="asp.net/images.aspx">查看上传的图片</a></div>
<div id="msg"></div>
<script language="javascript" type="text/javascript">
var imgUploader = new ImageUploader();
imgUploader.Config["PostUrl"] = "http://localhost/php/upload.php";
window.onload = function()
{
imgUploader.Init();
}
</script>
</body>
</html>
<%@ WebHandler Language="c#" Class="File_WebHandler" Debug="true" %>
using System;
using System.Web;
using System.IO;
//51aspx
public class File_WebHandler : IHttpHandler
{
private const int UploadFileLimit = 3;//上传文件数量限制
private string _msg = "上传成功!";//返回信息
public void ProcessRequest(HttpContext context)
{
string user = context.Request.UrlReferrer.ToString();
int iTotal = context.Request.Files.Count;
if (iTotal == 0)
{
_msg = "没有数据";
}
else
{
int iCount = 0;
for (int i = 0; i < iTotal; i++)
{
HttpPostedFile file = context.Request.Files[i];
if (file.ContentLength > 0 || !string.IsNullOrEmpty(file.FileName))
{
//保存文件
file.SaveAs(System.Web.HttpContext.Current.Server.MapPath("./file/" + Path.GetFileName(file.FileName)));
//这里可以根据实际设置其他限制
if (++iCount > UploadFileLimit)
{
_msg = "超过上传限制:" + UploadFileLimit;
break;
}
}
}
}
context.Response.Write("<script>window.parent.Finish('" + _msg + "');</script>");
}
public bool IsReusable
{
get
{
return false;
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>无标题页</title>
<link href="http://www.svnhost.cn/style/public.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script language="javascript">
function $(o){return document.getElementById(o);}
function CheckImgCss(o,img)
{
if (!/\.((jpg)|(bmp)|(gif)|(png))$/ig.test(o.value))
{
alert('只能上传jpg,bmp,gif,png格式图片!');
o.outerHTML = o.outerHTML;
}
else
{
$(img).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=o.value;
//$('Image1').src = o.value;//这里IE7已经不支持了。所以才有上面的方法。
}
}
</script>
<form id="form1" runat="server">
<div><h1>一次上传多个图片并预览,请选择图片:</h1>
<br />
<asp:FileUpload ID="FileUpload1" onchange="CheckImgCss(this, 'img');" runat="server" />
<br />
<asp:FileUpload ID="FileUpload2" onchange="CheckImgCss(this, 'img');" runat="server" />
<br />
<asp:FileUpload ID="FileUpload3" onchange="CheckImgCss(this, 'img');" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
<div id="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=<%= pic%>,sizingMethod=scale);width:102px;height:100px;"></div>
</div>
</form>
<div>
</body>
</html>
//该变量用来修改的的时候的默认值。例如上传自己的头像,如果用户修改头像,这里可以显示他原来的头像。
public string pic = "http://www.svnhost.cn/images/logo.gif";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Random r = new Random();
//这样循环,可以同时上传多个文件。前台已经有文件格式的判断,有错误提示了。这里只要过滤掉非法文件即可,无需提示了。
for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].ContentLength > 0)
{
string ex = System.IO.Path.GetExtension(Request.Files[i].FileName).ToLower();
if (".jpg.gif.png.bmp".Contains(ex))
{
string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + r.Next(100, 999).ToString() + ex;
//保存文件名到数据库
//xxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxx
Request.Files[i].SaveAs(Server.MapPath(newFileName));
pic = newFileName;
}
}
}
}