欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net多图片上传实现程序代码

程序员文章站 2024-03-06 19:28:50
前台代码如下:复制代码 代码如下:<% @ page language="c#" codefile="uploadimg.aspx.cs" inherits="net...
前台代码如下:
复制代码 代码如下:

<% @ page language="c#" codefile="uploadimg.aspx.cs" inherits="netadmin_apicture_uploadimg" %>
<!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>
<script type="text/javascript">
var i=1
function addfile()
{
if (i<8)
{var str = '<br> <input type="file" name="file" runat="server" style="width: 300px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" />'
document.getelementbyid('myfile').insertadjacenthtml("beforeend",str)
}
else
{
alert("您一次最多只能上传8张图片!")
}
i++
}
</script>
<link href="http://www.hzhuti.com/" rel="stylesheet" type="text/css" />
</head>
<body style="background-image: url(../images/bg.jpg); text-align: center">
<form id="form1" runat="server">
<div>
<table id="table1" align="center" border="0" cellpadding="1" cellspacing="1" class="table"
style="height: 58px" width="620">
<tr>
<td align="center" background="../images/topbg1.jpg">
<font color="#0000ff" face="宋体" size="3"><strong>上传图片</strong></font></td>
</tr>
<tr>
<td align="center">
</td>
</tr>
<tr>
<td align="center">
<asp:panel id="panel5" runat="server" width="608px">
<table width="100%">
<tr>
<td align="right" style="width: 100px">
</td>
<td align="left">
说明:点增加图片按钮可一次上传多张图片,可为每张图片写上一句不超过20个字的描述.单张图片大小不大于1024k</td>
</tr>
<tr>
<td align="right" style="width: 100px">
请选择图片:<br />
</td>
<td align="left"><p id="myfile"><input onclick="addfile()" type="button" value="增加图片(add)"><br />
<input type="file" name="file" runat="server" style="width: 300px"/>
描述:<input name="text" type="text" style="width: 150px" maxlength="20" />
</td>
</tr>
<tr>
<td align="right" style="width: 100px">
上传到的图库:</td>
<td align="left">
<asp:dropdownlist id="ddlalbum" runat="server" datasourceid="sqldatasource1"
datatextfield="albumname" datavaluefield="albumid">
</asp:dropdownlist><asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:webjakecs %>"
selectcommand="select [albumid], [albumname] from [wb_album] order by [albumid] desc">
</asp:sqldatasource>
</td>
</tr>
<tr>
<td align="right" style="width: 100px">
</td>
<td align="left">
<asp:button id="btnupload" runat="server" text="开始上传" onclick="btnupload_click" />
</td>
</tr>
<tr>
<td align="right" style="width: 100px">
</td>
<td align="left">
<asp:label id="lblmessage" runat="server" font-bold="true" forecolor="red"></asp:label></td>
</tr>
</table>
</asp:panel>
</td>
</tr>
<tr>
<td align="center">
<font face="宋体"></font><font face="宋体"> </font>
</td>
</tr>
<tr>
<td align="center">
<font face="宋体"></font>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

后台代码如下:
复制代码 代码如下:

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;
using system.io;
public partial class netadmin_apicture_uploadimg : system.web.ui.page
...{
protected void page_load(object sender, eventargs e)
...{
}
protected void btnupload_click(object sender, eventargs e)
...{
lblmessage.text = "";
lblmessage.visible = false;
system.web.httpfilecollection files = system.web.httpcontext.current.request.files;
system.text.stringbuilder strmsg = new system.text.stringbuilder("");
string[] rd = request.form[1].split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述
string albumid=ddlalbum.selectedvalue.trim();
int ifile;
for (ifile = 0; ifile < files.count; ifile++)
...{
if (files[ifile].filename.length > 0)
...{
system.web.httppostedfile postedfile = files[ifile];
if (postedfile.contentlength / 1024 > 1024)//单个文件不能大于1024k
...{
strmsg.append(path.getfilename(postedfile.filename) + "---不能大于1024k<br>");
break;
}
string fex = path.getextension(postedfile.filename);
if (fex != ".jpg" && fex != ".jpg" && fex != ".gif" && fex != ".gif")
...{
strmsg.append(path.getfilename(postedfile.filename) + "---图片格式不对,只能是jpg或gif<br>");
break;
}
}
}
if (strmsg.length <= 0)//说明图片大小和格式都没问题
...{
//以下为创建图库目录
string dirname = "pic00" + ddlalbum.selectedvalue.trim();
string dirpath = server.mappath("http://www.iiwnet.com/php");
dirpath = dirpath + "" + dirname;
if (directory.exists(dirpath) == false)
...{
directory.createdirectory(dirpath);
}
random ro = new random();
int name = 1;
for (int i = 0; i < files.count; i++)
...{
system.web.httppostedfile myfile = files[i];
string filename = "";
string fileextention = "";
string picpath = "";
filename = system.io.path.getfilename(myfile.filename);
string stro=ro.next(100,100000000).tostring()+name.tostring();//产生一个随机数用于新命名的图片
string newname =datetime.now.minute.tostring() + datetime.now.second.tostring()+datetime.now.millisecond.tostring()+stro;
if (filename.length > 0)//有文件才执行上传操作再保存到数据库
...{
fileextention = system.io.path.getextension(myfile.filename);
string ppath = dirpath + "" + newname + fileextention;
myfile.saveas(ppath);
string fjname = filename;
picpath = "picbase" + "" + dirname + "" + newname + fileextention;
}
addpicture(picpath, rd[i], albumid);//将图片信息保存到数据库
if (name == 1)//如果为每次更新的第一张图片,则将它更新为象册的封面
...{
upfirstimg(albumid, picpath);
}
name = name + 1;//用来重命名规则的变量
}
}
else
...{
lblmessage.text = strmsg.tostring();
lblmessage.visible = true;
}
}
private void addpicture(string imgpath,string imgnote,string albumid)
...{
string sql = "insert wb_albumimges(imgpath,imgnote,albumid) values('"+imgpath+"','"+imgnote+"','"+albumid+"')";
db mydb = new db();
mydb.runproc(sql);
}
private void upfirstimg(string albumid,string firstimg)
...{
string sql = "update wb_album set firstimg='"+firstimg+"' where albumid="+albumid;
db mydb = new db();
mydb.runproc(sql);
}
}