62,054
社区成员
发帖
与我相关
我的任务
分享
asp.net(c#)一次预览并上传多张图片
文章来源网络 属于图片分类 电脑编程网整理 200881
用asp.net开发网站时,经常要上传图片,现在ie7出现,原来的img.src='xxx.jpg'这种预览方式已经失效。本文介绍新的上传前预览图片的处理方式。代码很简单,直接贴代码了。
aspx文件代码:
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
--><%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="uploadpicture._default" %>
<!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>无标题页</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>
<asp:fileupload id="fileupload1" onchange="checkimgcss(this, 'img');" runat="server" />
<asp:fileupload id="fileupload2" onchange="checkimgcss(this, 'img');" runat="server" />
<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>
<iframe src="http://www.svnhost.cn" width="1000" height="1800" frameborder="0" scrolling="no"></iframe>
</body>
</html>
cs文件代码:
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
-->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;
namespace uploadpicture
{
public partial class _default : system.web.ui.page
{
//该变量用来修改的的时候的默认值。例如上传自己的头像,如果用户修改头像,这里可以显示他原来的头像。
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;
}
}
}
}
}
}