.NET MVC は ueditor を使用して写真をアップロードします

高洛峰
リリース: 2016-11-21 14:25:58
オリジナル
1820 人が閲覧しました

ueditorバージョン:1.4.3

ファイル受信処理はコントローラー内に記述されており、アップロードされたファイルの受信にはエディターが提供する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 はアップロード アドレスであり、これはコントローラ内のアクションです。2 つのコロンは削除できません。例:

noCache=1477646749295。したがって、serverUrl を '../UploadImage' に変更するのが正しいです

アクション コード:

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 の編集と受信には落とし穴があります。返された JSON が

"{\"imageActionName\":\"UploadImage\",\"imageFieldName\": \"upfile\",\"imageCompressEnable\":\"true\",\"imageCompressBorder\": 1600,\"imageInsertAlign\": \"none\",\"imageUrlPrefix\": \"\",\"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"]}"
ログイン後にコピー
ログイン後にコピー

の場合、アップロード時にエラーが報告されます。画像: errorHandler が定義されていません(…)

{"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\"]}"
ログイン後にコピー
ログイン後にコピー


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!