下面是本人写一个多文件上传的程序,希望对朋友们有帮助
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class upload_image : System.Web.UI.Page
{
private int FileSize=1000;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string msg = "";
int count=Request.Files.Count;
HttpPostedFile file;
string FileSavePath = "product_img/";
//检测
for (int i = 0; i < count; i++)
{
file = Request.Files[i];
//获取文件的后缀名
string fileType = file.ContentType.ToString().ToLower();
if (file.ContentLength == 0)
{
msg = "请选择图片文件";
LTP.Common.MessageBox.Show(msg);
return;
}
//文件类型判断
if (fileType != "image/gif" && fileType != "image/jpg" && fileType != "image/jpeg" && fileType != "image/pjpeg")
{
msg = "图片格式必须为jpg、gif、jpeg";
// LTP.Common.MessageBox.Show(msg);
LTP.Common.MessageBox.Show(msg);
return;
}
//文件大小比较
if (file.ContentLength > FileSize * 1024) // 限制要上传的文件大小(byte)。
{
msg = "图片文件不能大于" + FileSize + "K";
LTP.Common.MessageBox.Show(msg);
return;
}
}
for (int i = 0; i < count; i++)
{
file = Request.Files[i];
try
{
if (file.ContentLength > 0)
{
//上传文件
string fileExt = System.IO.Path.GetExtension(file.FileName).ToLower();
string filename = System.Guid.NewGuid().ToString() + fileExt;
string filePath = FileSavePath + filename;
string pfilePath = Server.MapPath(filePath);
file.SaveAs(pfilePath);
////取得图片大小
//HttpPostedFile myFile = file;//获取上传的文件
//int nFileLen = myFile.ContentLength;
//byte[] myData = new byte[nFileLen];
//myFile.InputStream.Read(myData, 0, nFileLen);
//FileStream newFile = new FileStream(pfilePath.ToString(), FileMode.Create);//创建文件
//newFile.Write(myData, 0, myData.Length);
//System.Drawing.Image img = System.Drawing.Image.FromStream(newFile);
//int intHeight = img.Height;//图片的高
//int intWidth = img.Width;//图片的宽度
////图片长宽判断
//if (intHeight > imgH + 10 || intHeight imgW + 10 || intWidth < imgW- 10)
//{
// msg = "上传图片宽度与高度应在" + imgW + "*" + imgH + "内!";
// LTP.Common.MessageBox.Show(msg);
// return;
//}
//保存数据库
// return;
}
}
catch (Exception ex)
{
msg = "网络繁忙,请稍后再试";
LTP.Common.MessageBox.Show(msg);
return;
}
}
}
}
转载于:https://www.cnblogs.com/intermediateprogramer/archive/2009/11/21/1607849.html