.NET MVC 使用ueditor上传图片
程序员文章站
2022-04-13 18:03:11
...
ueditor版本:1.4.3
文件接收处理写在controller,不使用编辑器提供的ashx接收上传文件
编辑器实例化,因为不同页面的所需编辑器功能是不一样的,实例化的时候传入配置参数:
var editor = new baidu.editor.ui.Editor({ toolbars: [["date", "time", "horizontal", "anchor", "spechars", "blockquote", "pagebreak", "bold", "italic", "underline", "strikethrough", "forecolor", "backcolor", "justifyleft", "justifycenter", "justifyright", "justifyjustify", "directionalityltr", "directionalityrtl", "indent", "removeformat", "autotypeset", "formatmatch", "pasteplain"], ["customstyle", "paragraph", "rowspacingbottom", "rowspacingtop", "lineheight", "fontfamily", "fontsize", "imagenone", "inserttable", "deletetable", "mergeright", "mergedown", "splittorows"], ["splittocols", "splittocells", "mergecells", "insertcol", "insertrow", "deletecol", "deleterow", "insertparagraphbeforetable", "fullscreen", "source", "undo", "redo", "insertunorderedlist", "insertorderedlist", "unlink", "link", "cleardoc", "selectall", "searchreplace", "separate", 'simpleupload'] ], serverUrl: '../UploadImage' }); editor.render("Content");
serverUrl为上传地址,即controller里的action,两个冒号不能去掉。举个栗子:
noCache=1477646749295。所以serverUrl改为'../UploadImage'才是正确的
action代码:
public ActionResult UploadImage() { var action = Request["action"]; var json = ""; if (action == "config") { json =@"{""imageActionName"":""UploadImage"",""imageFieldName"": ""upfile"",""imageCompressEnable"":""true"",""imageCompressBorder"": 1600,""imageInsertAlign"": ""none"",""imageUrlPrefix"": """",""imageAllowFiles"": ["".png"", "".jpg"", "".jpeg"", "".gif"", "".bmp""]}"; } else { var file= Request.Files["upfile"]; var relativePath = AppConfig.GetAppSettingsValue("CustomizeProductMaskImageRelativePath"); var newFileName = string.Concat(DateTime.Now.ToString("yy-MM-dd"), Path.GetExtension(file.FileName)); var savePath = Server.MapPath(relativePath); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } relativePath = Path.Combine(relativePath, newFileName); // 合成目标文件路径 var srcFileName = FilePath.CombinePath(savePath, newFileName); // 保存图片 file.SaveAs(srcFileName); var tvcMallImageUrl = ""; // 上传图片到外网服务器 tvcMallImageUrl = ""; json = json + "{\"url\":\"" + tvcMallImageUrl+"\","; json = json + "\"state\":\"SUCCESS\"}"; } return new ContentResult { ContentEncoding = Encoding.UTF8, ContentType = "application/json", Content = json }; }
编辑接收返回json有一处坑,如果返回的是
"{\"imageActionName\":\"UploadImage\",\"imageFieldName\": \"upfile\",\"imageCompressEnable\":\"true\",\"imageCompressBorder\": 1600,\"imageInsertAlign\": \"none\",\"imageUrlPrefix\": \"\",\"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"]}"
上传图片的时候报错: errorHandler is not defined(…)
返回为
{"imageActionName":"UploadImage","imageFieldName": "upfile","imageCompressEnable":"true","imageCompressBorder": 1600,"imageInsertAlign": "none","imageUrlPrefix": "","imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"]}
才正常
以下几种返回json的姿势:
return Content(json, "application/json", Encoding.UTF8); return Json(json,"application/json",Encoding.UTF8,JsonRequestBehavior.AllowGet); return JavaScript(json); return new JsonResult() {ContentEncoding = Encoding.UTF8, ContentType = "application/json", Data = json,JsonRequestBehavior = JsonRequestBehavior.AllowGet}; return new ContentResult { ContentEncoding = Encoding.UTF8, ContentType = "application/json", Content = json };
1、3、5返回json在浏览器显示为
{"imageActionName":"UploadImage","imageFieldName": "upfile","imageCompressEnable":"true","imageCompressBorder": 1600,"imageInsertAlign": "none","imageUrlPrefix": "","imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"]}
其他的为
"{\"imageActionName\":\"UploadImage\",\"imageFieldName\": \"upfile\",\"imageCompressEnable\":\"true\",\"imageCompressBorder\": 1600,\"imageInsertAlign\": \"none\",\"imageUrlPrefix\": \"\",\"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"]}"
上一篇: 初学XML的基础知识-认识XML的作用
推荐阅读
-
.net core ajax使用EPPlus上传excle导入总结
-
在Asp.Net Core中配置使用MarkDown富文本编辑器实现图片上传和截图上传(开源代码.net core3.0)
-
VUE + UEditor 单图片跨域上传功能的实现方法
-
ASP.NET MVC实现批量文件上传
-
ASP.NET MVC Webuploader实现上传功能
-
详解使用asp.net mvc部分视图渲染html
-
asp.net mvc 实现文件上传带进度条的思路与方法
-
Android使用OkHttp上传图片的实例代码
-
linux部署.net core api并且实现上传图片
-
微信开发中使用微信JSSDK和使用URL.createObjectURL上传预览图片的不同处理对比