博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js 利用iframe和location.hash跨域解决办法,java图片上传回调JS函数跨域
阅读量:6989 次
发布时间:2019-06-27

本文共 3405 字,大约阅读时间需要 11 分钟。

hot3.png

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

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

 

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");			//			String ret="";			//log.info("ret===:"+ret);			out.print(ret);				}	}
中间html 转换用,关健

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

参考博文:

转载于:https://my.oschina.net/pangzhuzhu/blog/301794

你可能感兴趣的文章
Batsing的网页编程规范(HTML/CSS/JS/PHP)
查看>>
2017-2018-1 20155222 《信息安全系统设计基础》第十四周学习总结
查看>>
第五周作业——用状态转换图描绘复印机的行为
查看>>
[HNOI2002]营业额统计
查看>>
Maven学习总结(五)——聚合与继承
查看>>
落谷P3941 入阵曲
查看>>
Java 里的异常(Exception)详解
查看>>
结对开发 随机产生数组并求最大子数组的和
查看>>
微博mini for Windows Phone
查看>>
[PKUWC2018]随机算法
查看>>
两个基于openssl的https client例子
查看>>
libgdx初试
查看>>
课程作业
查看>>
CSS shapes布局
查看>>
vue中如何实现pdf文件预览?
查看>>
ios7官方推荐icon尺寸
查看>>
VUE项目的目录关系
查看>>
Android操作HTTP实现与服务器通信(转)
查看>>
5秒速记php数组排序函数
查看>>
【分享】Objective-C Runtime
查看>>