• WebClient实现kindeditor跨域上传图片


    尝试上传图片到一个单独的服务器,使用的是KindeEditor 4.1.10

    kindeditor上传图片原理是:
     
    上传按钮提交到iframe,生成1个form和1个iframe,form提交到(arget)iframe
    iframe的onload方法获取返回的值,然后调用配置回调方法afterUpload/afterError。
     
    第一次尝试
    页面A document.domain = "aaa.xx.com";
    图片服务器B document.domain = "bbb.xx.com";
    在同一个域内xx.xx.com,KindEditor图片能上传,但是没有返回信息。
     
    第二次尝试
    直接引用
     <script src="http:aaa.xx.com/kindeditor/kindeditor.js" type="text/javascript"></script>
    KindEditor还是能上传图片,却无法返回信息
     
    第三次把KindEditor直接扔到了服务器A,
    页面引用<script src="http:aaa.xx.com/kindeditor/kindeditor.js" type="text/javascript"></script>
    并在当前页面B新建ashx,Upload上传路径为本地的ashx通过webclient通知图片服务器的ashx并返回信息
     
    1.通过uploadJson: '/upload_json.ashx?action=News',将图片上传到本地ashx,并用webcliet通知图片服务器
     HttpPostedFile imgFile = context.Request.Files["imgFile"];//KIndEditor上传的文件
    //将imgFile转换成字节形式
            byte[] fileByte = new byte[imgFile.InputStream.Length];
            imgFile.InputStream.Read(fileByte, 0, fileByte.Length);
      //通过WebClient提交文件
    //本地地址做测试,端口不同,实现域不同
    //图片服务器 http://192.168.0.107:81/
    //本地 http://192.168.0.107:80/
            string pUrl = string.Format("http://192.168.0.107:81/");
            string postUrl = string.Format("{0}kindeditor/asp.net/upload_json.ashx?img={1}&path={2}&action={3}", pUrl, imgFile.FileName, path, action);
            System.Net.WebClient webClient = new System.Net.WebClient();
    //上传数据
            byte[] responseArray = webClient.UploadData(postUrl, "POST", fileByte);
            string result = System.Text.Encoding.Default.GetString(responseArray, 0, responseArray.Length);
    //对返回的数据进行根据KindEditor的样式进行json解析,需要的是Newtonsoft.dll
            hash bk = Newtonsoft.Json.JsonConvert.DeserializeObject<hash>(result);
            Hashtable hash = new Hashtable();
            hash["error"] = 0;
    //hash["error"]=bk.error;这样竟然无效 hash["url"] = string.Format("{0}{1}", pUrl, bk.url);

      图片服务器接受本地上传的二进制图片数据,并对传送过来的主体进行保存

    //获得图片
            System.Drawing.Image postImage = System.Drawing.Image.FromStream(context.Request.InputStream);
                   
            //保存
            System.Drawing.Bitmap bitmap_b = new System.Drawing.Bitmap(postImage);
            bitmap_b.Save(filePath);
    

    2.由于项目临时改变,图片和项目放在同一个服务器上,又做了调整

    物理路径具体化,比如

    SaveURL=@"http://192.168.0.107:81";

    SavePath=@"D:项目路径";

    //String dirPath = context.Server.MapPath(savePath);//相对路径
    String dirPath = savePath;//这里改为绝对路径

    删除文件的时候
    删除的图片是http://192.168.1.1/k/2012/1212.jmg
    通过判断是否存在http://头
    http://192.168.1.1/k/2012/1212.jpg转换为物理绝对路径"D:项目/k/2012/1212.jpg
     如果是外部引入的文件则返回原地址
     if (strPath.ToLower().StartsWith("http://"))
                {
                    if (strPath.IndexOf(ManagerUrl) >= 0)
                    {
                        strPath = strPath.Replace(ManagerUrl, ManagerPath);
                        return strPath;
                    }
                    else
                    {
                        return strPath;
                    }
                }
        }
    

      

    嗯,自己整理的也很凌乱,暂时记录下,下次慢慢完善。

     
     
    你将独自前行,你会遇到友好,也会遇到恶意,不管你多么善良,因为世间就是这样,不好,不坏.
  • 相关阅读:
    RNN 一对一
    js只保留整数,向上取整,四舍五入,向下取整等函数
    oracle中的decode的使用
    ORACLE里锁有以下几种模式,v$locked_object,locked_mode
    时间序列/信号处理开源数据集-转
    ORACLE常用数值函数、转换函数、字符串函数
    Oracle to_date()函数的用法
    java使double保留两位小数的多方法 java保留两位小数
    Oracle修改字段类型方法总结
    POI对Excel自定义日期格式的读取
  • 原文地址:https://www.cnblogs.com/jsdvkm/p/4528587.html
Copyright © 2020-2023  润新知