`

js 利用iframe和location.hash跨域解决办法,java图片上传回调JS函数跨域

 
阅读更多

文章来源:http://www.itnose.net/detail/6044149.html
更多文章:http://www.itnose.net/type/71.html

上图   上代码: IE ,firefix,chrome 测试通过

传回,利用,iframe,location.hash,js0传回,利用,iframe,location.hash,js1传回,利用,iframe,location.hash,js2

 

js :这个主页面,部分代码,

 

function submitUpload(id){
	$("#imgSrc" + id +"").attr("alt", "图片上传中……");
	var imgID = id;
	if(id>0){
		imgID = 1;
	}
	
	var form=document.getElementById("formImg" + imgID +"");	
	//form.action = getContextPath()+"/pc/sys/photoupload/singleup"; //必须先包含sys.js文件
	form.method = "post";		
	form.idnum.value = id;
	var uriUp=domainStr+"/pc/sys/photoupload/singleup";
	form.action = uriUp;
	
	//用于返回
	var currentHref=window.location.href;
	var subHref=currentHref.substring(0, currentHref.lastIndexOf("/"));
	var input_domain = document.createElement("input");
	input_domain.setAttribute("name", "currentDomain");
	input_domain.setAttribute("value", subHref+"/callback-up.html");
	input_domain.setAttribute("type", "hidden");
	form.appendChild(input_domain);
	form.submit();
	//如果已经存在的图不是原图,则把服务器中的图片删除
	var currentPicPath =  $("#imgUrl" + id +"").val();
	if(!contains(origPics, currentPicPath) && currentPicPath!=""){
		delImg(currentPicPath, true);//true 删除图片 
	}
};

// step2: 上传图片,后台回调 
function callback(message) {
	var id=message.id;
	if(message.status.code=="0"){
		var filePath=message.filePath;
		var dbFilePath=message.dbFilePath;
		$("#imgUrl" + id +"").attr("value", dbFilePath);
		$("#imgSrc" + id +"").attr("src", filePath);
	}else{
		if(id!=0){
			$("#imgSrc" + id).parent().remove();
		}
		_message(message.status.message); //上传错误提示
	}
};


 

服务端处理  主要代码:

 

 

	// -----------------------------------------------------------------------------------
	// 单张图片上传,返回路径
	// ----------------------------------------------------------------------------------
	@RequestMapping(value = "/singleup", method = RequestMethod.POST)
	public void SingleUp(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "suffixDir", required = true) String suffixDir,
			@RequestParam(required = false, value = "cutPhoto") boolean cutPhoto, @RequestParam(required = false, value = "merchantId") String merchantId) throws Exception
	{
		StringBuffer basePath=new StringBuffer(request.getScheme());
		basePath.append("://");
		basePath.append(request.getServerName());
		basePath.append(":");
		basePath.append(request.getServerPort());
		basePath.append(request.getContextPath());
		basePath.append("/");
		basePath.append(FileUpload.getRealFilePath()/*.substring(0,FileUpload.getRealFilePath().length()-1)*/);
		
		User user = super.getLoginUser(request).getUser();
		if (user != null)
		{
			
			String filePath = fileUploadService.singleUpload(request, user, suffixDir, Fs.FileUpLoadType.PHOTO, cutPhoto, merchantId);

			// 处理返回状态
			UpoladStatus result = getStatus(filePath);
			String PromptSize = "";
			if (UpoladStatus.类型无效或图片过大.toString().equals(result.toString()))
			{
				// 2^10=1024
				PromptSize = "(最大限制:" + (FileUpload.photoSize >> 20) + "MB)";
			}

			PrintWriter out = response.getWriter();
			response.setHeader("Cache-Control", "none-cache");
			String idnum = request.getParameter("idnum");
			String reutrnDate = "{'id':'" + idnum + "','filePath':'" + basePath.append(filePath).toString() + "','dbFilePath':'" + filePath + "','status':{'code':'" + result.ordinal() + "','message':'" + result.name()
					+ PromptSize + "'}}";
			String currentDomain = request.getParameter("currentDomain");
			//<script>parent.parent.callback(" + reutrnDate + ");</script>
			String ret="<iframe id=\"ifr\" src=\""+currentDomain+"#"+reutrnDate+"\"></iframe>";
			//log.info("ret===:"+ret);
			out.print(ret);
		
		}

	}

中间html 转换用,关健

 

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<script type="text/javascript">

//alert(document.domain);
var returndata=self.location.hash.substring(1);
var message = eval('(' + returndata + ')'); 
parent.parent.callback(message);

</script>
</body>
</html>

我不善长写讲程,真接看代码吧....

 

参考博文:http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html#m3

 

分享到:
评论

相关推荐

    利用location.hash实现跨域iframe自适应

    页面域关系: 主页面a.html所属域... 问题本质: js对跨域iframe访问问题,因为要控制a.html中iframe的高度和宽度就必须首先读取得到b.html的大小,A、B不属于同一个域,浏览器为了安全性考虑,使js跨域访问受限,读取不到b.

    window.location.hash解析

    NULL 博文链接:https://hugang357.iteye.com/blog/1833003

    小发现之浅谈location.search与location.hash的问题

    下面小编就为大家带来一篇小发现之浅谈location.search与location.hash的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    jquery.hash.min.js 和 jquery.ba-hashchange.min.js

    单页后退前进刷新 jquery.hash.min.js 和 jquery.ba-hashchange.min.js

    各种Hash函数(JAVA版)

    JS-Hash Function Value: " + ghl.JSHash(key)); System.out.println(" 3. PJW-Hash Function Value: " + ghl.PJWHash(key)); System.out.println(" 4. ELF-Hash Function Value: " + ghl.ELFHash(key)); System...

    前端常见跨域解决方案(全).mht

    分享转载:前端常见跨域解决...3、 location.hash + iframe 4、 window.name + iframe跨域 5、 postMessage跨域 6、 跨域资源共享(CORS) 7、 nginx代理跨域 8、 nodejs中间件代理跨域 9、 WebSocket协议跨域

    location.hash保存页面状态的技巧

    hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分)...在我们的项目中,写了一个很简单的JavaScript基类来处理location.hash从而保存页面状态,今天在此就分享给大家。 (本文的内容可能

    scrollHash.js:滚动到Blaock时,然后更改location.hash!

    scrollHash-Willie.Smith.Chen 描述 滚动到Blaock时,然后更改location.hash! ## Demo页面

    JavaScript使用HTML5的window.postMessage实现跨域通信例子

    利用iframe和location.hash,数据直接暴露在了url中,数据容量和类型都有限 3.Flash LocalConnection, 对象可在一个 SWF 文件中或多个 SWF 文件间进行通信, 只要 在同一客户端就行,跨应用程序, 可以跨域。...

    [removed].hash 属性使用说明

    比如http://domain/#admin的location.hash=”#admin”。利用这个属性值可以做一个非常有意义的事情。 很多人都喜欢收藏网页,以便于以后... 那么,怎么用location.hash来解决这两个问题呢?其实一点也不神秘。 比如,我

    hash.js:纯JavaScript中的哈希函数

    hash.js 只是一个自行车棚。 安装 npm install hash.js 用法 var hash = require ( 'hash.js' ) hash . sha256 ( ) . update ( 'abc' ) . digest ( 'hex' ) 选择性哈希用法 var sha512 = require ( 'hash.js/lib/...

    hash.js:hash.js 是一个处理 location.hash 的超小型框架

    哈希.js hash.js是一个处理的超小型框架得到 /* * Given the following url: http://www.example.net/#!&foo=bar&array[]=1&array[]=2 */hash ( 'foo' ) ; // 'bar'hash ( 'array' ) ; // ['1', '2']hash ( ) ; // {...

    基于java的开发源码-哈希计算工具 Java-hash.zip

    基于java的开发源码-哈希计算工具 Java-hash.zip 基于java的开发源码-哈希计算工具 Java-hash.zip 基于java的开发源码-哈希计算工具 Java-hash.zip 基于java的开发源码-哈希计算工具 Java-hash.zip 基于java的开发...

    jquery.hash:jQuery DOM元素哈希插件

    jquery.hash jQuery DOM元素哈希插件 支持的哈希方法 base64 md5 sha1 自己编译 如果要编译自己的版本或jquery.hash的修改 npm install node compile 抓取out/jquery.hash.min.js文件并将其插入! 例子 DOM元素...

    javascript函数的解释

    javascript函数的解释,解释了具体函数的功能,一、函数JavaScript函数集合 1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document-&gt;html-&gt;(head,body) 4.一个浏览器窗口中的DOM顺序是:...

    location和history对象

    NULL 博文链接:https://baihe747.iteye.com/blog/2152459

    信息安全原理与技术-第五章Hash函数和数字签名.ppt

    Hash函数和数字签名 Hash函数和数字签名 Hash函数和数字签名

Global site tag (gtag.js) - Google Analytics