首頁  >  文章  >  後端開發  >  .NET MVC 使用ueditor上傳圖片

.NET MVC 使用ueditor上傳圖片

高洛峰
高洛峰原創
2016-11-21 14:25:581866瀏覽

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"]}

返回為

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 };

才正常

以下幾種返回json的姿勢:

{"imageActionName":"UploadImage","imageFieldName": "upfile","imageCompressEnable":"true","imageCompressBorder": 1600,"imageInsertAlign": "none","imageUrlPrefix": "","imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"]}

1、3、5返回json在瀏覽器顯示為

"{\"imageActionName\":\"UploadImage\",\"imageFieldName\": \"upfile\",\"imageCompressEnable\":\"true\",\"imageCompressBorder\": 1600,\"imageInsertAlign\": \"none\",\"imageUrlPrefix\": \"\",\"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"]}"

其他的為rr

reee
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn