.NET MVC 使用ueditor上傳圖片
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
熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。

C#和.NET的關係是密不可分的,但它們不是一回事。 C#是一門編程語言,而.NET是一個開發平台。 C#用於編寫代碼,編譯成.NET的中間語言(IL),由.NET運行時(CLR)執行。

要開始C#.NET開發,你需要:1.了解C#的基礎知識和.NET框架的核心概念;2.掌握變量、數據類型、控制結構、函數和類的基本概念;3.學習C#的高級特性,如LINQ和異步編程;4.熟悉常見錯誤的調試技巧和性能優化方法。通過這些步驟,你可以逐步深入C#.NET的世界,並編寫高效的應用程序。
