Kindeditor跨域上传图片在ASP.NET方案中的解决

时间:2022-08-29 18:30:43

虽然标题写的是ASP.NET的解决方案,但是也可用在PHP,Jsp中。直接上代码

首先在客户端中配置

 var editor;
 document.domain = window.location.host;//js解析域
 KindEditor.ready(function (k) {
     editor = k.create('#editor_id',
       {
                uploadJson: GlobalConfig
                    .RestServiceUrl +
                    "/FileUpload/upload_json.ashx?folder=News&domainhost=" + window.location.host, // 'upload_json.ashx',
                fileManagerJson: GlobalConfig
                    .RestServiceUrl +
                    "/FileUpload/file_manager_json.ashx?folder=News", //'file_manager_json.ashx',
                allowFileManager: true,
                resizeType: 0,
                height: "500px", //编辑器的高度
                urlType: 'domain' //让上传的文件记录绝对路径!
      });

关键代码部分为

document.domain = window.location.host;//js解析域

服务器端配置为

 public void ProcessRequest(HttpContext context)
    {
        string folder = context.Request["folder"];
        _webhostdomain = context.Request["domainhost"];
        _returnstring = string.Format(@"<script   type='text/javascript'>document.domain = '{0}'</script>",_webhostdomain);
   ....
 context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
        context.Response.Write(_returnstring + JsonMapper.ToJson(hash));
        context.Response.End();
}

  类似于jsonp的方式解决