码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-05-18 06:52:17      阅读:548      评论:0      收藏:0      [点我收藏+]

标签:js   图片   服务器   javascript   callback   

奶奶的:折腾了我二天,终于解决了!网上有很多例子。

但跟我的都不太一样,费话不多说了,上图   上代码: IE ,firefix,chrome 测试通过

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

 

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

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

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

标签:js   图片   服务器   javascript   callback   

原文地址:http://blog.csdn.net/liangrui1988/article/details/26085195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!