.NET MVC는 ueditor를 사용하여 사진을 업로드합니다.

高洛峰
풀어 주다: 2016-11-21 14:25:58
원래의
1820명이 탐색했습니다.

ueditor 버전: 1.4.3

파일 수신 처리는 컨트롤러에 작성되며 업로드된 파일을 수신하기 위해 편집기에서 제공하는 ashx를 사용하지 않습니다.

Editor 인스턴스화로 인해 다른 페이지의 편집이 필요합니다. 인스턴스화할 때 구성 매개변수가 전달됩니다.

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은 컨트롤러의 작업인 업로드 주소입니다. . 예:

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 학습자의 빠른 성장을 도와주세요!