时间:2016-02-15 22:08 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net多图片上传实现程序代码】,下面是详细的讲解!
asp.net多图片上传实现程序代码
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);
}
}
关于asp.net多图片上传实现程序代码的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【页面打印】关于ASP.NET页面打印技术的常用方法
- 【MVC5】MVC 5 第一章 创建MVC 5 web应用程序-net
- 【MVC】一个简单MVC5 + EF6示例分享-EF6实例-MVC5
- 【服务器】asp.net页面状态管理cookie和服务器状
- 【asp】asp.net更新指定记录的方法-net--更新-指
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
