.NET MVC 使用ueditor上傳圖片

來源:互聯網
上載者:User
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\"]}"
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.