ASP.NET(C#)實(shí)現(xiàn)一次性動(dòng)態(tài)上傳多張圖片的代碼(多個(gè)文件)
先看效果:
打開的初始界面:

默認(rèn)是上傳一個(gè)圖片,但當(dāng)我們點(diǎn)“增加圖片”按鈕時(shí)可以實(shí)現(xiàn)選擇多個(gè)圖片及其描述同時(shí)上傳,本功能限制一次最多只能上傳8張,且每張圖片大小不超過(guò)1M,這個(gè)大家可根據(jù)實(shí)際情況更改!
如圖:

下面來(lái)看實(shí)現(xiàn)過(guò)程:
第一步,使用javascript代碼實(shí)現(xiàn)動(dòng)態(tài)添加文件上傳框和描述文本框,關(guān)鍵代碼如下:
<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>HTML調(diào)用代碼為:
<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" />
第二步:服務(wù)器端代碼實(shí)現(xiàn)
就上傳單個(gè)文件或圖片來(lái)說(shuō),使最普通不過(guò)的了,但是對(duì)于這樣的一次性上傳多個(gè)文件以及它們相應(yīng)的描述的問(wèn)題,就要費(fèi)點(diǎn)周折
首先,通過(guò)System.Web.HttpContext.Current.Request.Files方法獲取客戶端的文件集合,然后通過(guò)Request.Form方法獲得描述文本框集合,最后利用一個(gè)循環(huán)將文件上傳,并將相應(yīng)的信息保存到數(shù)據(jù)庫(kù)
關(guān)鍵代碼如下:
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
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)
...{
.......................... //上傳單個(gè)文件并保存相關(guān)信息
}
}
最后給出上述功能的全部代碼:
HTML代碼:
<%...@ 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>無(wú)標(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">
說(shuō)明:點(diǎn)增加圖片按鈕可一次上傳多張圖片,可為每張圖片寫上一句不超過(guò)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)//說(shuō)明圖片大小和格式都沒(méi)問(wèn)題
...{
//以下為創(chuàng)建圖庫(kù)目錄
string dirname = "pic00" + ddlAlbum.SelectedValue.Trim();
string dirpath = Server.MapPath("http://www.cnblogs.com/PicBase");
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;//用來(lái)重命名規(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(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實(shí)例
- Asp.net(C#)讀取數(shù)據(jù)庫(kù)并生成JS文件制作首頁(yè)圖片切換效果(附demo源碼下載)
- asp.net(c#)實(shí)現(xiàn)從sqlserver存取二進(jìn)制圖片的代碼
- asp.net(C#)壓縮圖片,可以指定圖片模板高寬
- asp.net(c#)獲取內(nèi)容第一張圖片地址的函數(shù)
- asp.net(c#)判斷遠(yuǎn)程圖片是否存在
- C# 將字節(jié)流轉(zhuǎn)換為圖片的實(shí)例方法
- C#識(shí)別出圖片里的數(shù)字和字母
- C#圖片按比例縮放的實(shí)現(xiàn)代碼
- c#實(shí)現(xiàn)圖片二值化例子(黑白效果)
- c#圖片縮放圖片剪切功能實(shí)現(xiàn)(等比縮放)
- asp.net(c#)編程實(shí)現(xiàn)將彩色圖片變灰階圖片的方法示例
相關(guān)文章
ASP.NET Core 集成 React SPA應(yīng)用的步驟
這篇文章主要介紹了ASP.NET Core 集成 React SPA應(yīng)用的步驟,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04基于ASP.NET Core數(shù)據(jù)保護(hù)生成驗(yàn)證token示例
本篇文章主要介紹了基于ASP.NET Core數(shù)據(jù)保護(hù)生成驗(yàn)證token,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02.Net?Core實(shí)現(xiàn)第三方QQ掃碼登錄
這篇文章介紹了.Net?Core實(shí)現(xiàn)第三方QQ掃碼登錄的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02WPF實(shí)現(xiàn)文本描邊+外發(fā)光效果的示例代碼
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)文本描邊以及外發(fā)光的效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-03-03ASP.NET:把a(bǔ)shx寫到類庫(kù)里并在頁(yè)面上調(diào)用的具體方法
最近在調(diào)整博客的架構(gòu),進(jìn)一步把表現(xiàn)和業(yè)務(wù)分離,所以要把之前用ashx搞的那些Http Handler放到類庫(kù)中,下面是具體的步驟及代碼2013-06-06手把手教你AspNetCore WebApi數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了手把手教你AspNetCore WebApi數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10asp.net 安全的截取指定長(zhǎng)度的html或者ubb字符串
在將html代碼輸出到頁(yè)面時(shí),有時(shí)候會(huì)需要截?cái)嘧址A糁付ㄩL(zhǎng)度的字符串,由于html中有些標(biāo)簽必須成對(duì)出現(xiàn),所以在截取html時(shí)需要特別注意,不能因?yàn)榻財(cái)鄦?wèn)題把頁(yè)面搞亂掉。2010-01-01在一個(gè)網(wǎng)站下再以虛擬目錄的方式掛多個(gè)網(wǎng)站的方法
在一個(gè)網(wǎng)站下再以虛擬目錄的方式掛N個(gè)網(wǎng)站的方法2010-04-04