会员
周边
新闻
博问
闪存
众包
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
中级程序员
博客园
首页
新随笔
联系
管理
订阅
图片上传
下面是本人写一个多文件上传的程序,希望对朋友们有帮助 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload_image.aspx.cs" Inherits="upload_image" %>
删除
删除
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; } } } }
大家好,有空到我网站看看
聚风尚
,给点意见
posted @
2009-11-21 23:37
中级程序员
阅读(
489
) 评论(
2
)
收藏
举报
刷新页面
返回顶部
公告