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,
width: '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,
width: '100%',
height: '170px',
afterBlur: function () {
this.sync();
},
extraFileUploadParams: {
item_id: ,
id: xgid
}
});
//重新布局异步刷新
cx(xgid);
})
//后台
//======================图片上传=======================//
[HttpPost]
public void UploadImage()
{
var jdid = int.Parse(Request.Form["id"].ToString());
//文件保存路径
var savePath = string.Empty;
var savePath2 = string.Empty;
var jdcx = db.hotel.Find(jdid);
if (jdcx.lx == )
{
savePath = "~/img/" + jdcx.gsid + "/" + jdid + "/fwbtp/";
savePath2 = "../../img / " + jdcx.gsid + "/" + jdid + "/fwbtp/ ";
}
else
{
savePath = "~/img/" + jdid + "/fwbtp/";
savePath2 = "../../img/" + jdid + "/fwbtp/";
}
//文件保存路径
//string savePath = "/img/"+xgid;
//const string savePath = "/Content/Images/";
//上传文件类型
string fileTypes = "gif,jpg,jpeg,png,bmp";
//上传文件大小 限制大小1M 一张图片
int maxSize = ;
var hash = new Hashtable();
//获取文件信息
HttpPostedFileBase file = Request.Files["imgFile"];
if (file == null)
{
hash = new Hashtable();
hash["error"] = ;
hash["message"] = "请选择文件";
Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
Response.Write(JsonMapper.ToJson(hash));
}
string dirPath = Server.MapPath(savePath);
if (!Directory.Exists(dirPath))
{
//不存在该目录则创建一个
Directory.CreateDirectory(dirPath);
//hash = new Hashtable();
//hash["error"] = 1;
//hash["message"] = "上传目录不存在";
//return Json(hash, "text/html;charset=UTF-8");
}
string fileName = file.FileName;
string fileExt = Path.GetExtension(fileName).ToLower();
//文件大小判断
if (file.InputStream == null || file.InputStream.Length > maxSize)
{
hash = new Hashtable();
hash["error"] = ;
hash["message"] = "上传文件大小超过限制";
Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
Response.Write(JsonMapper.ToJson(hash));
}
if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring().ToLower()) == -)
{
hash = new Hashtable();
hash["error"] = ;
hash["message"] = "上传文件扩展名是不允许的扩展名";
Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
Response.Write(JsonMapper.ToJson(hash));
}
var newFileName = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + fileExt;
var filePath = dirPath + newFileName;
file.SaveAs(filePath);
var fileUrl = savePath2 + newFileName;
hash = new Hashtable();
hash["error"] = ;
hash["url"] = fileUrl;
Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
Response.Write(JsonMapper.ToJson(hash));
}