kindeditor跨域上传文件php,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图片能上传,但是没有返回信息。

第二次尝试

直接引用

KindEditor还是能上传图片,却无法返回信息

第三次把KindEditor直接扔到了服务器A,

页面引用

并在当前页面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(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;

}

}

}

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

原文:http://www.cnblogs.com/jsdvkm/p/4528587.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值