如何上传文件夹(本例失败)

说明:因为asp.net没有直接选取文件夹的控件(如果有的话请告诉我),所以上传整个文件夹的思路是通过上传一个具体文件,
通过得到的文件名得到文件夹的路径。然后遍历该文件夹得到所有文件,再上传处理。这里确实可以得到本地文件名。
该代码是我帮助一个网友的(说要是周六解决不了,星期天还要加班,心里不忍,所以花点时间),现在发博给需要的朋友一个参考。
(很遗憾经过测试,因为Fileupload控件不支持上传文件夹,你无法给它赋值。由于出于安全性原因,不可能直接在浏览器上通过代码直接上传本地文件夹,必须通过ActiveX控件才能实现,笔者也收到很多这样的控件,但是一般都是英文的,而且是费用都在200美刀左右!) 因此,使用本方法上传文件将会发现你只能上传你所选择的那个文件!!!
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.IO;
  12. using web.Webutility;
  13. using System.Drawing;
  14. using System.Drawing.Drawing2D;
  15. using web.BLL;
  16. using web.Model;
  17. public partial class manage_Photo_Save : System.Web.UI.Page
  18. {
  19.     protected void Page_Load(object sender, EventArgs e)
  20.     {
  21.     }
  22.     //读取某个文件夹内的所以文件
  23.     private void get_files(string filePath)
  24.     {
  25.         try
  26.         {
  27.             string[] files_path = Directory.GetFiles(filePath);
  28.             foreach (string file_path in files_path)
  29.             {
  30.             }
  31.         }
  32.         catch (Exception ex)
  33.         {
  34.             Console.WriteLine("errors: " + ex.Message);
  35.         }
  36.     }
  37.     //上传图片
  38.     protected void btnPhoto_Click(object sender, EventArgs e)
  39.     {
  40.         if (file.PostedFile.FileName == "" || file.PostedFile.FileName == string.Empty)
  41.         {
  42.             Script.Alert("请选择数据源!");
  43.             return;
  44.         }
  45.         else
  46.         {
  47.             string filePath = file.PostedFile.FileName;                               //获取文件路径
  48.             int lenght = filePath.LastIndexOf("//");
  49.             filePath = filePath.Substring(0, lenght);//解析出文件所在的文件夹路径
  50.             string code = string.Empty;
  51.             string[] files = Directory.GetFiles(filePath);                               //一个一个取出文件夹里的文件
  52.             foreach (string f in files)
  53.             {
  54.                 string img = f;
  55.                 int leng = img.LastIndexOf("//");
  56.                 int len = img.LastIndexOf(".");
  57.                 string jpg = img.Substring(len + 1, (img.Length - len - 1));             //图片后缀名
  58.                 string imgName = img.Substring(leng + 1, (len - leng - 1));              //图片名称
  59.                 Response.Write("客户端图片路径:" + f + "<br />");
  60.                 if (jpg.Equals("JPG") || jpg.Equals("jpg")||jpg.Equals ("JPEG") && imgName.Length > 4)//文件名如0524111.jpg
  61.                 {
  62.                     try
  63.                     {
  64.                         Int64 jpgName = Convert.ToInt64(imgName);//注意:因为这里的图片的是用数字命名的
  65.                         string first = imgName.Substring(0, 1);                          //图书代号第一位
  66.                         string second = imgName.Substring(1, 2);                         //图书代号第二位
  67.                         string fullPath2 = @"E:/weblzj/" + first + "/" + second + "/";  //创建文件夹
  68.                         string fullPath = "~/" + first + "/" + second + "/";
  69.                         string[] arr = fullPath.Split('/');
  70.                         string urlstr = null;
  71.                         for (int i = 0; i < arr.Length - 1; i++)
  72.                         {
  73.                             urlstr = urlstr + arr[i].ToString() + "/";
  74.                         }
  75.                         if (urlstr != null)
  76.                         {
  77.                             if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(urlstr)) == false)
  78.                             {
  79.                                 try
  80.                                 {
  81.                                     Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(urlstr));
  82.                                 }
  83.                                 catch { HttpContext.Current.Response.Write("必须使用根目录 或 权限不够"); return; }
  84.                             }
  85.                         }
  86.                         string all = fullPath2 + imgName + "." + jpg;
  87.                         int count = uploadPhoto(imgName, fullPath);    //把图片保存到服务器            
  88.                         if (count > 0)
  89.                         {
  90.                             // this.caiJian(all, 80, 117); //创建缩略图
  91.                             //PhotoInfo info = InsertPhoto(imgName);//写入数据库
  92.                         }
  93.                     }
  94.                     catch
  95.                     {
  96.                         code += imgName;
  97.                     }
  98.                 }
  99.             }
  100.             // Response.Redirect("Success.aspx?url=Photo_Save.aspx&text=上传图片成功!");
  101.         }
  102.     }
  103.     //保存图片
  104.     private int uploadPhoto(string jpgName, string fullPath)
  105.     {
  106.         Int64 code = Convert.ToInt64(jpgName);
  107.         int i = 0;
  108.         try
  109.         {
  110.             string serverPath = string.Empty;
  111.             serverPath = Server.MapPath(fullPath + jpgName + ".jpg");
  112.             this.file.SaveAs("服务器保存路径:" + serverPath);
  113.             Response.Write(serverPath);
  114.             this.file.Dispose();
  115.             i = 1;
  116.             Response.Write(serverPath + "<br />");
  117.             if (cbIsTrue.Checked == true)                                           //直接覆盖原来的图片
  118.             {
  119.                 this.file.SaveAs(serverPath);
  120.                 i = 1;
  121.             }
  122.             else                                                                    //不覆盖原来的图片
  123.             {
  124.                 PhotoInfo photo = new PhotoInfo();
  125.                photo = new BPhoto().Onephoto(code);
  126.                 if (photo.Pho_ID < 1)                                               //不存在
  127.                 {
  128.                     this.file.SaveAs(serverPath);
  129.                     i = 1;
  130.                 }
  131.                else
  132.                 {
  133.                     i = 0;
  134.                 }
  135.             }
  136.         }
  137.         catch
  138.         {
  139.             i = 0;
  140.         }
  141.         return i;
  142.     }
  143.     private static PhotoInfo InsertPhoto(string jpgName)
  144.     {
  145.         PhotoInfo photo = new PhotoInfo();
  146.         photo.Pho_ID = 0;
  147.         photo.Pho_Code = Convert.ToInt64(jpgName);
  148.         photo.Pho_UserId = "lzj_3976";
  149.         photo.Pho_Time = DateTime.Now;
  150.         new BPhoto().Insert(photo, true);
  151.         return photo;
  152.     }
  153.     public void caiJian(string imgPath, int width, int height)
  154.     {
  155.         string path = imgPath;
  156.         int leng = path.LastIndexOf("/");
  157.         string filePath = path.Substring(0, leng);
  158.         int len = path.LastIndexOf(".");
  159.         string jpg = path.Substring(len + 1, (path.Length - len - 1)); //图片后缀名
  160.         string imgName = path.Substring(leng + 1, (len - leng - 1)); //图片名称
  161.         string newImgName = imgName + "_." + jpg;
  162.         string newPath = filePath + "/" + newImgName;
  163.         System.Drawing.Image newImg = System.Drawing.Image.FromFile(path);
  164.         int oldwidth = newImg.Width;
  165.         int oldheight = newImg.Height;
  166.         //新建一个bmp图片
  167.         System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height);
  168.         //新建一个画板
  169.         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  170.         //设置高质量插值法
  171.         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  172.         //设置高质量,低速度呈现平滑程度
  173.         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  174.         // 清空画布并以透明背景色填充
  175.         g.Clear(System.Drawing.Color.White);
  176.         // 在指定位置并且按指定大小绘制原图片的指定部分
  177.         g.DrawImage(newImg, new System.Drawing.Rectangle(0, 0, width, height),
  178.             new System.Drawing.Rectangle(0, 0, oldwidth, oldheight),
  179.             System.Drawing.GraphicsUnit.Pixel);
  180.         try
  181.         {
  182.             // 以jpg格式保存缩略图
  183.             newImg.Dispose();
  184.             bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  185.             bitmap.Dispose();
  186.             g.Dispose();
  187.             newImg.Dispose();
  188.         }
  189.         catch (System.Exception e)
  190.         {
  191.             GC.Collect();
  192.             Response.Write(e.Message + e.StackTrace);
  193.         }
  194.         finally
  195.         {
  196.             newImg.Dispose();
  197.             bitmap.Dispose();
  198.             g.Dispose();
  199.         }
  200.     }
  201. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值