说明:因为asp.net没有直接选取文件夹的控件(如果有的话请告诉我),所以上传整个文件夹的思路是通过上传一个具体文件,
通过得到的文件名得到文件夹的路径。然后遍历该文件夹得到所有文件,再上传处理。这里确实可以得到本地文件名。
该代码是我帮助一个网友的(说要是周六解决不了,星期天还要加班,心里不忍,所以花点时间),现在发博给需要的朋友一个参考。
(很遗憾经过测试,因为Fileupload控件不支持上传文件夹,你无法给它赋值。由于出于安全性原因,不可能直接在浏览器上通过代码直接上传本地文件夹,必须通过ActiveX控件才能实现,笔者也收到很多这样的控件,但是一般都是英文的,而且是费用都在200美刀左右!) 因此,使用本方法上传文件将会发现你只能上传你所选择的那个文件!!!
- 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;
- using web.Webutility;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using web.BLL;
- using web.Model;
- public partial class manage_Photo_Save : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- //读取某个文件夹内的所以文件
- private void get_files(string filePath)
- {
- try
- {
- string[] files_path = Directory.GetFiles(filePath);
- foreach (string file_path in files_path)
- {
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("errors: " + ex.Message);
- }
- }
- //上传图片
- protected void btnPhoto_Click(object sender, EventArgs e)
- {
- if (file.PostedFile.FileName == "" || file.PostedFile.FileName == string.Empty)
- {
- Script.Alert("请选择数据源!");
- return;
- }
- else
- {
- string filePath = file.PostedFile.FileName; //获取文件路径
- int lenght = filePath.LastIndexOf("//");
- filePath = filePath.Substring(0, lenght);//解析出文件所在的文件夹路径
- string code = string.Empty;
- string[] files = Directory.GetFiles(filePath); //一个一个取出文件夹里的文件
- foreach (string f in files)
- {
- string img = f;
- int leng = img.LastIndexOf("//");
- int len = img.LastIndexOf(".");
- string jpg = img.Substring(len + 1, (img.Length - len - 1)); //图片后缀名
- string imgName = img.Substring(leng + 1, (len - leng - 1)); //图片名称
- Response.Write("客户端图片路径:" + f + "<br />");
- if (jpg.Equals("JPG") || jpg.Equals("jpg")||jpg.Equals ("JPEG") && imgName.Length > 4)//文件名如0524111.jpg
- {
- try
- {
- Int64 jpgName = Convert.ToInt64(imgName);//注意:因为这里的图片的是用数字命名的
- string first = imgName.Substring(0, 1); //图书代号第一位
- string second = imgName.Substring(1, 2); //图书代号第二位
- string fullPath2 = @"E:/weblzj/" + first + "/" + second + "/"; //创建文件夹
- string fullPath = "~/" + first + "/" + second + "/";
- string[] arr = fullPath.Split('/');
- string urlstr = null;
- for (int i = 0; i < arr.Length - 1; i++)
- {
- urlstr = urlstr + arr[i].ToString() + "/";
- }
- if (urlstr != null)
- {
- if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(urlstr)) == false)
- {
- try
- {
- Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(urlstr));
- }
- catch { HttpContext.Current.Response.Write("必须使用根目录 或 权限不够"); return; }
- }
- }
- string all = fullPath2 + imgName + "." + jpg;
- int count = uploadPhoto(imgName, fullPath); //把图片保存到服务器
- if (count > 0)
- {
- // this.caiJian(all, 80, 117); //创建缩略图
- //PhotoInfo info = InsertPhoto(imgName);//写入数据库
- }
- }
- catch
- {
- code += imgName;
- }
- }
- }
- // Response.Redirect("Success.aspx?url=Photo_Save.aspx&text=上传图片成功!");
- }
- }
- //保存图片
- private int uploadPhoto(string jpgName, string fullPath)
- {
- Int64 code = Convert.ToInt64(jpgName);
- int i = 0;
- try
- {
- string serverPath = string.Empty;
- serverPath = Server.MapPath(fullPath + jpgName + ".jpg");
- this.file.SaveAs("服务器保存路径:" + serverPath);
- Response.Write(serverPath);
- this.file.Dispose();
- i = 1;
- Response.Write(serverPath + "<br />");
- if (cbIsTrue.Checked == true) //直接覆盖原来的图片
- {
- this.file.SaveAs(serverPath);
- i = 1;
- }
- else //不覆盖原来的图片
- {
- PhotoInfo photo = new PhotoInfo();
- photo = new BPhoto().Onephoto(code);
- if (photo.Pho_ID < 1) //不存在
- {
- this.file.SaveAs(serverPath);
- i = 1;
- }
- else
- {
- i = 0;
- }
- }
- }
- catch
- {
- i = 0;
- }
- return i;
- }
- private static PhotoInfo InsertPhoto(string jpgName)
- {
- PhotoInfo photo = new PhotoInfo();
- photo.Pho_ID = 0;
- photo.Pho_Code = Convert.ToInt64(jpgName);
- photo.Pho_UserId = "lzj_3976";
- photo.Pho_Time = DateTime.Now;
- new BPhoto().Insert(photo, true);
- return photo;
- }
- public void caiJian(string imgPath, int width, int height)
- {
- string path = imgPath;
- int leng = path.LastIndexOf("/");
- string filePath = path.Substring(0, leng);
- int len = path.LastIndexOf(".");
- string jpg = path.Substring(len + 1, (path.Length - len - 1)); //图片后缀名
- string imgName = path.Substring(leng + 1, (len - leng - 1)); //图片名称
- string newImgName = imgName + "_." + jpg;
- string newPath = filePath + "/" + newImgName;
- System.Drawing.Image newImg = System.Drawing.Image.FromFile(path);
- int oldwidth = newImg.Width;
- int oldheight = newImg.Height;
- //新建一个bmp图片
- System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height);
- //新建一个画板
- System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
- //设置高质量插值法
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
- //设置高质量,低速度呈现平滑程度
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- // 清空画布并以透明背景色填充
- g.Clear(System.Drawing.Color.White);
- // 在指定位置并且按指定大小绘制原图片的指定部分
- g.DrawImage(newImg, new System.Drawing.Rectangle(0, 0, width, height),
- new System.Drawing.Rectangle(0, 0, oldwidth, oldheight),
- System.Drawing.GraphicsUnit.Pixel);
- try
- {
- // 以jpg格式保存缩略图
- newImg.Dispose();
- bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
- bitmap.Dispose();
- g.Dispose();
- newImg.Dispose();
- }
- catch (System.Exception e)
- {
- GC.Collect();
- Response.Write(e.Message + e.StackTrace);
- }
- finally
- {
- newImg.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- }
- }