Kindeditor JS 富文本编辑器图片上传指定路径

js

//==================
    KindEditor.ready(function (K) {
        var hotelid = $("#hotelid").val();
        window.editor1 = K.create('#jdxx', {
            //上传管理
            uploadJson: '/Hotelgl/UploadImage?id=' + hotelid,
            //文件管理
            fileManagerJson: '/Hotelgl/UploadImage?id='+hotelid ,
            allowFileManager: false,
             '100%',
            height: '170px',
            afterBlur: function () {
                this.sync();
            },
//hotelid 酒店id值 extraFileUploadParams: { item_id:
1000, id: hotelid } }); });
   $("#jdjscx").click(function () {
        var xgid = $("#hotel option:selected").attr("value");
        KindEditor.remove('#jdxx'); // 先移除之前创建的编辑器
        editor1 = KindEditor.create('#jdxx', {  // 再重新创建编辑器
              uploadJson: '/Hotelgl/UploadImage?id=' + xgid,
              fileManagerJson: '/Hotelgl/UploadImage?id=' + xgid,
              allowFileManager: false,
               '100%',
              height: '170px',
              afterBlur: function () {
                  this.sync();
              },
              extraFileUploadParams: {
                  item_id: 1000,
                  id: xgid
              }
        });
        //重新布局异步刷新
        cx(xgid);
      
    })

 

 

//后台

 1  //======================图片上传=======================//
 2         [HttpPost]
 3         public void UploadImage()
 4         {
 5             var jdid = int.Parse(Request.Form["id"].ToString());
 6             //文件保存路径
 7             var savePath = string.Empty;
 8             var savePath2 = string.Empty;
 9             var jdcx = db.hotel.Find(jdid);
10 
11             if (jdcx.lx == 3)
12             {
13                 savePath = "~/img/" + jdcx.gsid + "/" + jdid + "/fwbtp/";
14                 savePath2 = "../../img / " + jdcx.gsid + "/" + jdid + "/fwbtp/ ";
15 
16             }
17             else
18             {
19                 savePath = "~/img/" + jdid + "/fwbtp/";
20                 savePath2 = "../../img/" + jdid + "/fwbtp/";
21             }
22 
23             //文件保存路径
24             //string savePath = "/img/"+xgid;
25             //const string savePath = "/Content/Images/";
26             //上传文件类型
27             string fileTypes = "gif,jpg,jpeg,png,bmp";
28 
29             //上传文件大小 限制大小1M 一张图片
30             int maxSize = 1000000;
31 
32             var hash = new Hashtable();
33             //获取文件信息
34             HttpPostedFileBase file = Request.Files["imgFile"];
35             if (file == null)
36             {
37                 hash = new Hashtable();
38                 hash["error"] = 1;
39                 hash["message"] = "请选择文件";
40                 Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
41                 Response.Write(JsonMapper.ToJson(hash));
42             }
43 
44             string dirPath = Server.MapPath(savePath);
45             if (!Directory.Exists(dirPath))
46             {
47                 //不存在该目录则创建一个
48                 Directory.CreateDirectory(dirPath);
49                 //hash = new Hashtable();
50                 //hash["error"] = 1;
51                 //hash["message"] = "上传目录不存在";
52                 //return Json(hash, "text/html;charset=UTF-8");
53             }
54 
55             string fileName = file.FileName;
56             string fileExt = Path.GetExtension(fileName).ToLower();
57             //文件大小判断
58             if (file.InputStream == null || file.InputStream.Length > maxSize)
59             {
60                 hash = new Hashtable();
61                 hash["error"] = 1;
62                 hash["message"] = "上传文件大小超过限制";
63                 Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
64                 Response.Write(JsonMapper.ToJson(hash));
65             }
66 
67             if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
68             {
69                 hash = new Hashtable();
70                 hash["error"] = 1;
71                 hash["message"] = "上传文件扩展名是不允许的扩展名";
72                 Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
73                 Response.Write(JsonMapper.ToJson(hash));
74             }
75 
76             var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + fileExt;
77             var filePath = dirPath + newFileName;
78             file.SaveAs(filePath);
79             var fileUrl = savePath2 + newFileName;
80 
81             hash = new Hashtable();
82             hash["error"] = 0;
83             hash["url"] = fileUrl;
84             Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
85             Response.Write(JsonMapper.ToJson(hash));
86         }
原文地址:https://www.cnblogs.com/h5monkey/p/6247722.html