表结构:
tb_pic
ID (int)主键自增
pic (varchar(max)) 上传图片的路径
xxxx.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadImg.aspx.cs" Inherits="Web.Services.UploadImg" %>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>批量上传图片</title>
<link rel="stylesheet" type="text/css" href="../Common/css/PopWinStyle.css" />
<script src="../Common/js/PopWin.js" type="text/javascript"></script>
<script type="text/javascript">
var i = 1;
function addFile() {
if (i < 6) {
var str = '<input type="file" name="File" runat="server" style="width: 300px"/>'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd", str);
}
else {
alert("您一次最多只能上传6张图片!");
}
i++;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="Pagetitle">
<i class="icon-align-justify"></i>图片上传
</div>
<div>
<asp:Button ID="btnUpload" runat="server" Text="开始上传" OnClick="btnUpload_Click" />
<input οnclick="addFile()" type="button" value="添加图片" /><br /> //单击动态添加选择图片路径
</div>
<div>
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
</div>
<div>
<table id="MyFile">
<%--<tr>
<td style="text-align: right; width: 80px">图片:</td>
<td style="text-align: left">
<div style="border: 1px solid #808080;">
<asp:Image ID="img" runat="server" Width="160px" Height="120px" οnclick="ShowPControl('图片管理', '../Sel/ImageInfo.aspx?type=1&ci=625', 536, 438, 'SetImg')" /></div>
<asp:HiddenField ID="hfImgID" runat="server" Value="0" />
<asp:HiddenField ID="hfUrl" runat="server" />
</td>
</tr>--%>
<tr >
<td>
<input type="file" name="File" style="width: 300px" /> //选择图片路径,第一次默认加载一个选择路径
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
前台效果图:
加上自己想要的css
后台代码:
xxxx.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DAL;
using System.Data.SqlClient;
using System.Data;
namespace Web.Services
{
public partial class UploadImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Visible = false;
}
/// <summary>
/// 上传照片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
lblMessage.Visible = true;
string FileName = "", PicPath = "";
int ifile;
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
for (ifile = 0; ifile < files.Count; ifile++)
{
if (files[ifile].FileName.Length > 0)
{
System.Web.HttpPostedFile postedfile = files[ifile];
if (postedfile.ContentLength / 1024 > 512)//单个文件不能大于512k
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "不能大于512k!<br>");
break;
}
string fex = Path.GetExtension(postedfile.FileName);
if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF" && fex != ".bmp" && fex != ".BMP")
{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "图片格式仅支持jpg,gif,bmp!<br>");
break;
}
}
else
{
strmsg.Append("没有图片!");
break;
}
}
if (strmsg.Length <= 0)//图片大小和格式都没问题
{
//创建图库目录
string dirname = "pic";
string dirpath = Server.MapPath("/");
dirpath = dirpath + @"/" + dirname;
if (Directory.Exists(dirpath) == false)
{
Directory.CreateDirectory(dirpath);
}
for (int i = 0; i < files.Count; i++)
{
System.Web.HttpPostedFile myFile = files[i];
FileName = System.IO.Path.GetFileName(files[i].FileName);
if (FileName.Length > 0)//存在上传文件
{
string ppath = dirpath + @"/" + FileName;
myFile.SaveAs(ppath);
PicPath += FileName + "|";//拼接图片目录
}
}
AddPicture(PicPath);
}
else
{
lblMessage.Text = strmsg.ToString();
}
}
/// <summary>
/// 将图片路径保存到数据库
/// </summary>
/// <param name="imgpath"></param>
private void AddPicture(string imgpath)
{
string sql = "insert into " + SqlDataBase.DataBase1 + @"tb_pic values('" + imgpath + "')"; //这些事框架自带,用自己的方法实现即可
int result = SqlHelp.ExecuteSql(new SqlConnection(), new SqlCommand(), sql); //这些事框架自带,用自己的方法实现即可
if (result > 0)
{
lblMessage.Text = "成功!";
}
else
{
lblMessage.Text = "失败!";
}
}
}
}