asp.net多圖片上傳實(shí)現(xiàn)程序代碼
更新時(shí)間:2012年09月05日 19:48:37 作者:
下面是一個(gè)完整的asp.net同時(shí)支持多圖片上傳一個(gè)實(shí)現(xiàn),有需要的朋友可參考一下,本文章限制同時(shí)可上傳8張圖片,當(dāng)然大可自己可修改更多或更少
前臺(tái)代碼如下:
<% @ 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>無標(biāo)題頁(yè)</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 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">
說明:點(diǎn)增加圖片按鈕可一次上傳多張圖片,可為每張圖片寫上一句不超過20個(gè)字的描述.單張圖片大小不大于1024k</td>
</tr>
<tr>
<td align="right" style="width: 100px">
請(qǐng)選擇圖片:<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">
上傳到的圖庫(kù):</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>
后臺(tái)代碼如下:
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(',');//獲得圖片描述的文本框字符串?dāng)?shù)組,為對(duì)應(yīng)的圖片的描述
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)//單個(gè)文件不能大于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) + "---圖片格式不對(duì),只能是jpg或gif<br>");
break;
}
}
}
if (strmsg.Length <= 0)//說明圖片大小和格式都沒問題
...{
//以下為創(chuàng)建圖庫(kù)目錄
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();//產(chǎn)生一個(gè)隨機(jī)數(shù)用于新命名的圖片
string NewName =DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+stro;
if (FileName.Length > 0)//有文件才執(zhí)行上傳操作再保存到數(shù)據(jù)庫(kù)
...{
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);//將圖片信息保存到數(shù)據(jù)庫(kù)
if (name == 1)//如果為每次更新的第一張圖片,則將它更新為象冊(cè)的封面
...{
upFirstimg(albumid, PicPath);
}
name = name + 1;//用來重命名規(guī)則的變量
}
}
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);
}
}
復(fù)制代碼 代碼如下:
<% @ 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>無標(biāo)題頁(yè)</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 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">
說明:點(diǎn)增加圖片按鈕可一次上傳多張圖片,可為每張圖片寫上一句不超過20個(gè)字的描述.單張圖片大小不大于1024k</td>
</tr>
<tr>
<td align="right" style="width: 100px">
請(qǐng)選擇圖片:<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">
上傳到的圖庫(kù):</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>
后臺(tái)代碼如下:
復(fù)制代碼 代碼如下:
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(',');//獲得圖片描述的文本框字符串?dāng)?shù)組,為對(duì)應(yīng)的圖片的描述
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)//單個(gè)文件不能大于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) + "---圖片格式不對(duì),只能是jpg或gif<br>");
break;
}
}
}
if (strmsg.Length <= 0)//說明圖片大小和格式都沒問題
...{
//以下為創(chuàng)建圖庫(kù)目錄
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();//產(chǎn)生一個(gè)隨機(jī)數(shù)用于新命名的圖片
string NewName =DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+stro;
if (FileName.Length > 0)//有文件才執(zhí)行上傳操作再保存到數(shù)據(jù)庫(kù)
...{
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);//將圖片信息保存到數(shù)據(jù)庫(kù)
if (name == 1)//如果為每次更新的第一張圖片,則將它更新為象冊(cè)的封面
...{
upFirstimg(albumid, PicPath);
}
name = name + 1;//用來重命名規(guī)則的變量
}
}
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);
}
}
您可能感興趣的文章:
- asp.net圖片上傳生成縮略圖的注意事項(xiàng)
- asp.net 將圖片上傳到mysql數(shù)據(jù)庫(kù)的方法
- ASp.net下fckeditor配置圖片上傳最簡(jiǎn)單的方法
- Asp.net 2.0 無刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
- Asp.net圖片上傳實(shí)現(xiàn)預(yù)覽效果的簡(jiǎn)單代碼
- KindEditor圖片上傳的Asp.net代碼實(shí)例
- asp.net圖片上傳實(shí)例
- ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
- asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)
- ASP.NET圖片上傳實(shí)例(附源碼)
相關(guān)文章
C# 文件保存到數(shù)據(jù)庫(kù)中或者從數(shù)據(jù)庫(kù)中讀取文件
在編程中我們常常會(huì)遇到“將文件保存到數(shù)據(jù)庫(kù)中”這樣一個(gè)問題,雖然這已不是什么高難度的問題,但對(duì)于一些剛剛開始編程的朋友來說可能是有一點(diǎn)困難。2009-03-03
ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件實(shí)例
本篇文章主要介紹了ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
使用.net?core?自帶DI框架實(shí)現(xiàn)延遲加載功能
在某些情況,我們希望能延遲一個(gè)依賴的初始化。如果使用的是autofac,我們可以通過注入Lazy來實(shí)現(xiàn),這篇文章主要介紹了使用.net?core?自帶DI框架實(shí)現(xiàn)延遲加載,需要的朋友可以參考下2023-02-02
用ASP.NET做的個(gè)性化的郵件發(fā)送系統(tǒng)
如果要你用ASP來做一個(gè)郵件發(fā)送系統(tǒng),你一定認(rèn)為這是一個(gè)比較復(fù)雜的工作。其實(shí)也的確是這樣。但當(dāng)他的后繼產(chǎn)品ASP.NET被推出以后,他的強(qiáng)大功能就使的這一切就變的相對(duì)簡(jiǎn)單了。真的這樣神奇么?我們就通過ASP.NET做一個(gè)郵件發(fā)送系統(tǒng),看看到底有什么奧秘,是不是真的簡(jiǎn)單。2008-02-02
LazyCaptcha自定義隨機(jī)驗(yàn)證碼和字體的示例詳解
LazyCaptcha是仿EasyCaptcha和SimpleCaptcha,基于.Net?Standard?2.1的圖形驗(yàn)證碼模塊,這篇文章主要介紹了LazyCaptcha自定義隨機(jī)驗(yàn)證碼和字體?,需要的朋友可以參考下2022-03-03
在ASP.NET MVC項(xiàng)目中集成SignalR可以實(shí)現(xiàn)定時(shí)任務(wù)操作數(shù)據(jù)庫(kù)并將數(shù)據(jù)實(shí)時(shí)更新到網(wǎng)頁(yè),通過創(chuàng)建新項(xiàng)目、配置SignalR、操作數(shù)據(jù)庫(kù)、創(chuàng)建SignalR Hub和定時(shí)任務(wù),可以實(shí)現(xiàn)前端頁(yè)面的實(shí)時(shí)數(shù)據(jù)顯示,本文提供了詳細(xì)的步驟和代碼示例,幫助開發(fā)者快速實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)更新功能2024-09-09
asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法,涉及asp.net操作office文件的相關(guān)技巧,需要的朋友可以參考下2016-08-08

