图片上传

下面是本人写一个多文件上传的程序,希望对朋友们有帮助
删除
删除
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值