首页后端开发ASP.NETasp.net多图片上传实现程序代码

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

时间2024-02-01 09:57:03发布访客分类ASP.NET浏览671
导读:收集整理的这篇文章主要介绍了asp.net多图片上传实现程序代码,觉得挺不错的,现在分享给大家,也给大家做个参考。 前台代码如下:复制代码 代码如下:<% @ Page Langu...
收集整理的这篇文章主要介绍了asp.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 (i8)
{ 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) + "---不能大于1024kbr> ");
break;
}
string fex = Path.GetExtension(postedfile.FileName);
if (fex != ".jpg" & & fex != ".JPG" & & fex != ".gif" & & fex != ".GIF")
...{
strmsg.Append(Path.GetFileName(postedfile.FileName) + "---图片格式不对,只能是jpg或gifbr> ");
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);
}
}
您可能感兴趣的文章:
  • asp.net图片上传生成缩略图的注意事项
  • asp.net 将图片上传到mysql数据库的方法
  • ASp.net下fckeditor配置图片上传最简单的方法
  • Asp.net 2.0 无刷新图片上传 显示缩略图 具体实现
  • Asp.net图片上传实现预览效果的简单代码
  • KindEditor图片上传的Asp.net代码实例
  • asp.net图片上传实例
  • ASP.NET简单好用功能齐全图片上传工具类(水印、缩略图、裁剪等)
  • asp.net文件上传解决方案(图片上传、单文件上传、多文件上传、检查文件类型)
  • ASP.NET图片上传实例(附源码)

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

上一篇: ASP.NET对路径"xxxxx"...下一篇:用Html5与Asp.net MVC上传多个文...猜你在找的ASP.NET相关文章 C# 一些面试试题的实例教程2022-05-16.NET 6开发TodoList应用之请求日志组件HttpLogging介绍2022-04-16.NET 6中间件Http Logging使用介绍2022-04-16gojs一些实用的高级用法2022-04-16.NET6开发TodoList应用之实现查询排序2022-04-16.NET6开发TodoList应用之实现数据塑形2022-04-16.NET微服务架构CI/CD自动打包镜像2022-04-16Asp.Net Core 使用Monaco Editor 实现代码编辑器功能2022-04-16.NET微服务架构CI/CD自动构建Jenkins+Gitee2022-04-16.Net Core微服务网关Ocelot集成Consul2022-04-16 其他相关热搜词更多phpjavapython程序员loadpost-format-gallery

若转载请注明出处: asp.net多图片上传实现程序代码
本文地址: https://pptw.com/jishu/595249.html
vue diff算法全解析 ASP.NET Cache的一些总结分享

游客 回复需填写必要信息