多文件上传

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="js/jquery.min.js"></script>
    </head>
    <body>
        <form id="uploadForm" enctype="multipart/form-data">
            <input id="file" type="file" name="file"  multiple="multiple" />
            <button id="upload" type="button">upload</button>
        </form>
    </body>
    <script>
    $("#upload").click(function () {
                $.ajax({
                    url: 'http://localhost:8081/upload0',
                    type: 'POST',
                    cache: false,
                    data: new FormData($('#uploadForm')[0]),
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        console.log(data)
                        alert(data.msg)
                    }
                }).fail(function (res) {
                    console.log(res)
                    alert("系统错误")
                });
            });
    </script>
</html>

//后台

public JsonResult upload_img_Add1(string id1)
        {
            lock (lock_tx)
            {
                string code = "";
                string src = "";
                string msg = "";
                string name = "";
                List<string> urls = new List<string>();
                try
                {
                    int id2 = 0;
                    int.TryParse(id1, out id2);
                    var item = Common.freeSqlHelper.fsqlDb.Select<Model.party_member.metting>().Where(a => a.ID == id2).First();
                    if (item == null)
                    {
                        code = "0";
                        msg = "未能找到该会议!";
                    }
                    else
                    {
                        string userid = get_userid();
                        if (userid == "")
                        {
                            code = "0";
                            msg = "未找到当前登录用户!";
                        }
                        else
                        {
                            string xm = get_name1(userid);
                            var u2 = Common.freeSqlHelper.fsqlDb.Select<Model.sys.Users>().Where(a => a.userid == userid).First();
                            if (u2 == null)
                            {
                                code = "0";
                                msg = "未找到该用户!";
                            }
                            else
                            {
                                var files = Request.Files; //获取选中文件  
                                for(int i = 0; i < files.Count; i++)
                                {
                                    try
                                    {
                                        var file = files[i];
                                        var filecombin = file.FileName.Split('.');
                                        string houzhui = filecombin[filecombin.Length - 1];
                                        if (file == null || String.IsNullOrEmpty(file.FileName) || file.ContentLength == 0 || filecombin.Length < 2)
                                        {
                                            code = "0";
                                            msg = "文件错误!";
                                        }
                                        if (houzhui == "jpg" || houzhui == "jpeg" || houzhui == "png" || houzhui == "gif")
                                        {
                                            //定义本地路径位置
                                            string local = "Upload\\detail_lx_img1";
                                            string filePathName = string.Empty;
                                            string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, local);

                                            var tmpName = Server.MapPath("~/Upload/detail_lx_img1/ ");
                                            var tmp = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";// file.FileName;

                                            var tmpIndex = 0;
                                            //判断是否存在相同文件名的文件 相同累加1继续判断
                                            //while (System.IO.File.Exists(tmpName + tmp))
                                            //{
                                            //    tmp = filecombin[0] + "_" + ++tmpIndex + "." + filecombin[1];
                                            //}

                                            //不带路径的最终文件名
                                            filePathName = tmp;

                                            if (!System.IO.Directory.Exists(localPath))
                                                System.IO.Directory.CreateDirectory(localPath);
                                            string localURL = Path.Combine(local, filePathName);
                                            file.SaveAs(Path.Combine(localPath + "\\temp", filePathName));   //保存图片(文件夹)


                                            string r3 = "..\\" + local + "\\temp" + "\\" + filePathName;
                                            string r2 = "..\\" + local + "\\" + filePathName;
                                            if (MakeThumbnail(r3, r2, 1000, 0) == true)
                                            {
                                                int px1 = 1;
                                                Model.party_member.metting_img m = freeSqlHelper.fsqlDb.Select<Model.party_member.metting_img>().Where(a => a.metting_id == id2).OrderByDescending(a => a.px).First();
                                                if (m != null)
                                                {
                                                    int.TryParse(m.px.ToString(), out px1);
                                                    px1++;
                                                }

                                                Model.party_member.metting_img m_img = new Model.party_member.metting_img();
                                                m_img.img_src = "../" + localURL.Trim().Replace("\\", "/");
                                                m_img.metting_id = id2;
                                                m_img.px = px1;


                                                var t5 = Common.freeSqlHelper.fsqlDb.Insert(m_img).IgnoreColumns(a => a.ID).ExecuteIdentity();//忽略掉自增列(ID)并返回自增值
                                                if (t5 > 0)
                                                {
                                                    code = "1";
                                                    src = "../" + localURL.Trim().Replace("\\", "/");
                                                    name = Path.GetFileNameWithoutExtension(file.FileName);   // 获取文件名不含后缀名
                                                    msg = "上传成功";
                                                    DeleteImgFile(r3);
                                                    urls.Add(src);
                                                    Common.Class1.SetLog(userid, get_name1(userid), "对模块图片进行了添加,图片id为" + t5);
                                                }
                                                else
                                                {
                                                    code = "0";
                                                    msg = "上传图片失败!";
                                                }
                                            }
                                            else
                                            {
                                                code = "0";
                                                msg = "文件转换失败!";
                                            }
                                        }
                                        else
                                        {
                                            code = "0";
                                            msg = "文件必须是图片格式!";
                                        }
                                    }
                                    catch(Exception ex1)
                                    {

                                    }
                                    
                                }
                                
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    code = "0";
                    msg = ex.Message;
                }
                return Json(new { code = code, msg = msg, name = name, urls = urls }, JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源文件地址</param>
        /// <param name="thumbnailPath">目标文件地址</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode = "W")
        {

            //生成缩略图
            try
            {

                Image originalImage = Image.FromFile(Server.MapPath(originalImagePath));

                int towidth = width;
                int toheight = height;

                int x = 0;
                int y = 0;
                int ow = originalImage.Width;
                int oh = originalImage.Height;

                switch (mode)
                {
                    case "HW"://指定高宽缩放(可能变形)                
                        break;
                    case "W"://指定宽,高按比例                    
                        toheight = originalImage.Height * width / originalImage.Width;
                        break;
                    case "H"://指定高,宽按比例
                        towidth = originalImage.Width * height / originalImage.Height;
                        break;
                    case "Cut"://指定高宽裁减(不变形)                
                        if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                        {
                            oh = originalImage.Height;
                            ow = originalImage.Height * towidth / toheight;
                            y = 0;
                            x = (originalImage.Width - ow) / 2;
                        }
                        else
                        {
                            ow = originalImage.Width;
                            oh = originalImage.Width * height / towidth;
                            x = 0;
                            y = (originalImage.Height - oh) / 2;
                        }
                        break;
                    default:
                        break;
                }

                //新建一个bmp图片
                Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

                //新建一个画板
                Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //清空画布并以透明背景色填充
                g.Clear(Color.Transparent);

                //在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
          new Rectangle(x, y, ow, oh),
          GraphicsUnit.Pixel);

                try
                {
                    //以jpg格式保存缩略图
                    bitmap.Save(Server.MapPath(thumbnailPath), System.Drawing.Imaging.ImageFormat.Jpeg);
                    return true;
                }
                catch (System.Exception e)
                {
                    // throw e;
                    return false;
                }
                finally
                {
                    originalImage.Dispose();
                    bitmap.Dispose();
                    g.Dispose();
                }

            }
            catch (Exception ex)
            {
                string r10 = ex.Message;
                return false;

            }

        }
        private void DeleteImgFile(string fileUrl)
        {
            //删除指定的文件
            string file = System.Web.HttpContext.Current.Server.MapPath(fileUrl);
            if (System.IO.File.Exists(file))
            {
                 System.IO.File.Delete(file);
            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值