ASP.NET webform的FileUpload控件的多图片上传到服务器
前言:
单图片上传不在概述,多图片上传的主要一点就是设置FileUpload的multiple=“multiple”,即可。
话不多说直接上代码
第一步直接拖控件,如下图所示
后台代码 PYth是上传的服务端路径,filename上传的文件夹名称,这个是我自己定义的
你们可以用的我==标记的部分,包括截取文件名,文件格式
public bool Upload(string pyth,string filename)
{
== System.IO.DirectoryInfo DirInfo = new DirectoryInfo(pyth);==
== DirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;==
== HttpFileCollection upFiles = Request.Files;==
if (upFiles.Count > 0)
{
int count = 0;
int iPos;
int iFlag = -1;
string strExtType;
int imgcode=0;//图片的code值
string imagename = "";
string realimageame = "";//图片名字
string Lithologicbatchnumber = Guid.NewGuid().ToString();//图片批次编号
List<Totallithology> Totallithologylist = new List<Totallithology>();
==for (int i = 0; i < upFiles.Count; i++)==
{
string extendName = string.Empty;
string datestr = DateTime.Now.Ticks.ToString() + i;
HttpPostedFile postedFile = upFiles[i];
if (postedFile.ContentLength > 0)
{
==string imageame = postedFile.FileName;==
iPos = imageame.LastIndexOf('.');
if (iPos >= 0)
{
//.txt .doc .ppt .pdf .html .xls .xlsx
strExtType = imageame.Substring(iPos);
}
#region 老的处理方式
iFlag = imageame.LastIndexOf("\\");
if (iFlag >= 0)
{
== imagename = imageame.Substring(iFlag + 1);==
string rty = imagename.Substring(1, 4);
== realimageame = imagename.Replace(".gif", "");==
==imgcode =int.Parse(rty.Replace("0", ""));==
}
#endregion
extendName = Path.GetExtension(postedFile.FileName.ToLower().Replace(".", ""));
== string savePath = pyth + imagename;//上传图片路径==
==postedFile.SaveAs(savePath);==
count = count + 1;
System.Drawing.Image img = System.Drawing.Image.FromFile(savePath);
if (img.Width <= 100 && img.Height <= 100)
{
FileInfo fi = new FileInfo(savePath);
FileStream fs = fi.OpenRead();
byte[] picBytes = new byte[fs.Length];
fs.Read(picBytes, 0, Convert.ToInt32(fs.Length));
Totallithology single = new Totallithology();
single.Lithologyfilename = filename;
single.Lithologicbatchnumber = Lithologicbatchnumber;
single.LithologyCode =imgcode;
single.LithologyImagename =realimageame;
single.LithologyImage = picBytes;
Totallithologylist.Add(single);
}
}
}