(1) jsp代码:
<form id="form" name="form" enctype="multipart/form-data" method="post" target="hidden_frame"> <table style="border:0;width:100%;text-align:middle;"> <tr style="border:0;height:20px;width:100%;"> <td style="border:0;width:75px;height:20px;line-height:20px;">控件标识</td> <td> <input id="viewkey" name="viewkey" style="width:200px;" type="text" maxlength="300" placeholder="控件标识不能超过30个字。" class="easyui-validatebox" onkeyup="check()" required="true" missingMessage="参数名称不可为空."/> </td> </tr> <tr> <td>更新类型</td> <td><input id="type" name="type" class="easyui-combobox" valueField="id" textField="text" panelHeight="auto"/></td> </tr> <tr> <td>备注</td> <td colspan=3> <textarea id="remark" name="remark" onkeyup="check()" placeholder="备注不能超过300个字。" style="width:200px;height:80px;" ></textarea> </td> </tr> <tr> <td>资源文件:</td> <td > <input type="file" id="file" name="file" style="height:25px; width:200px;" onchange="fileChange(this);"> <input type="hidden" name="projectid" id="projectid" > <input type="hidden" name="downimageconfigid" id="downimageconfigid" > <input type="hidden" name="iskeychange" id="iskeychange" > <input type="hidden" name="isnopic" id="isnopic" > <iframe name="hidden_frame" id="hidden_frame" style="display:none"></iframe> </td> </tr> <tr> <td></td> <td><div style="color: red; margin-top: 10px;">图片大小必须小于500K。</div></td> </tr> </table> </form>
说明:form中的target指向iframe中的name。这点要注意。
(2) js代码:
//添加对话框
function initDialog(){
$('#imgconf-dialog').dialog({
modal:true,
closable:false,
top: 20,
buttons:[{
id:'ut_add',
text:'确定',
iconCls:'icon-ok',
handler:function(){
//表单注册事件
$('#form').form({
success:function(data){//提交成功后的回调函数
if(data === '00'){
jqueryAlert('操作成功');
}
if(data === '03'){
$.messager.alert(global.title,'主键为空!','warning');
$('#ut_add').linkbutton('enable');
return;
}
if(data === '02'){
$.messager.alert(global.title,'已存在的控件标识!','warning');
$('#ut_add').linkbutton('enable');
return;
}
if(data === '01'){
$.messager.alert(global.title,'操作失败','warning');
$('#ut_add').linkbutton('enable');
return;
}
$('#imgconf-dialog').dialog('close');
//重新加载列表
getDataGridData();;
}
});
$('#ut_add').linkbutton('disable');
//【添加】
if(global.operatype == 'add'){
if($('#viewkey').val() == null || $('#viewkey').val() == ''){
$.messager.alert(global.title,'您尚未输入控件标识!','warning');
$('#ut_add').linkbutton('enable');
return;
}
if($('#file').val() == ''){
$.messager.alert(global.title,'您尚未上传图片!!','warning');
$('#ut_add').linkbutton('enable');
return;
}
//表单上传操作
$('#projectid').val(global.projectid);
$('#form').attr("action", global.web_path + "/grid/imgconf/addimgconf.do");
$("#form").submit();
$('#ut_add').linkbutton('disable');
} else {//【编辑】
//控件标识是否改变
var iskeychange;
if(selected.viewkey == $('#viewkey').val()){//控件标识没有改变
iskeychange = 'no';
}else{
iskeychange = 'yes';
}
var isnopic;
if($('#file').val() == ''){//是否有上传图片
snopic = 'yes';
}else{
isnopic = 'no';
}
//表单上传操作
$('#projectid').val(global.projectid);
$('#downimageconfigid').val(selected.downimageconfigid);
$('#iskeychange').val(iskeychange);
$('#isnopic').val(isnopic);
$('#form').attr("action",global.web_path + "/grid/imgconf/modimgconf.do");
$("#form").submit(); ;
$('#ut_add').linkbutton('disable');
}
}
},{
id:'ut_close',
text:'退出',
handler:function(){
$('#ut_add').linkbutton('enable');
$('#imgconf-dialog').dialog('close');
$('#uploadify').uploadifyClearQueue();
}
}]
});
}
//重置
function reset(){
$('#ut_add').linkbutton('enable');
var target = $('#file');
if(global.operatype == 'mod'){
$('#imgconf-dialog').dialog('setTitle','修改');
$('#viewkey').val(selected.viewkey);
$('#type').combobox('setValue', selected.type);
$('#remark').val(selected.remark);
$('#imgconf-dialog').dialog('open');
//文件上传清空
deleteFile('file');
}else {
$('#imgconf-dialog').dialog('setTitle','添加');
$('#viewkey').val('');
$('#remark').val('');
//文件上传清空
deleteFile('file');
}
}
/**
* 文本区域限制长度
*/
function check(){
var content = $('#remark').val();
len = content.length;
var maxlen = 300;
if(len > maxlen){
alert("字数太长,已被截断为300字!");
$('#remark').val(content.substr(0,maxlen));
}
}
// input type='file'置位操作
function deleteFile(file){
var ie = (navigator.appVersion.indexOf("MSIE")!=-1);//IE
var ff = (navigator.userAgent.indexOf("Firefox")!=-1);//Firefox
if(ie){
refreshUploader($("input[name="+file+"]")[0]);
}
else{
$("input[name="+file+"]").attr("value","");
}
}
function refreshUploader(file){
var file2= file.cloneNode(false);
file2.onchange= file.onchange;
file.parentNode.replaceChild(file2,file);
}
//检测文件大小和类型
function fileChange(target){
//检测上传文件的类型
if(!(/("只允许上传jpg|gif|png|jpeg格式的图片");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else target.value="";//for FF,Chrome,Safari
return;
} else {
return; //alert("ok");//or you can do nothing here.
}
//检测上传文件的大小
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
var fileSize = 0;
if (isIE && !target.files){
var filePath = target.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
} else {
fileSize = target.files[0].size;
}
var size = fileSize / 1024;
if(size>(500)){
alert("文件大小不能超过500KB");
if(window.ActiveXObject) {//for IE
target.select();//select the file ,and clear selection
document.selection.clear();
} else if(window.opera) {//for opera
target.type="text";target.type="file";
} else {
target.value="";//for FF,Chrome,Safari
}
return;
}else{
return;
}
}
(3) 后台java代码:
/**
* 添加下载图片配置
* @throws IOException
*/
@RequestMapping(value = "/grid/imgconf/addimgconf",method = {RequestMethod.POST, RequestMethod.GET},produces = {"text/html;charset=UTF-8"})
@ResponseBody
public String addImgConf(HttpServletRequest
request, HttpServletResponse response, BindException errors) throws IOException{
String m = "00";
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
response.setHeader("Access-Control-Allow-Origin", "*");
String projectid = Function.dealNull(multipartRequest.getParameter("projectid"));
String viewKey = Function.dealNull(multipartRequest.getParameter("viewkey"));
String type = Function.dealNull(multipartRequest.getParameter("type"));
String remark = Function.dealNull(multipartRequest.getParameter("remark"));
try {
//唯一性判断
int size = imgConfService.getImgConfsCount(Constants.DEF_STRING_NULL, projectid, viewKey, Constants.STATUS_ON);
if(size > 0){
m = "02";
logger.info("已存在的控件标识!");
return "<textarea>" + m + "</textarea>";
}
//上传图片
Map<String, String> picInfo = imgConfService.uploadImage(fileMap);
imgConfService.addImgConf(UUID.randomUUID().toString(), projectid, viewKey, type, remark, picInfo.get("URL"), Constants.STATUS_ON);
m = "00";
logger.info("添加下载图片配置成功!");
} catch (Exception e) {
m = "01";
logger.error("添加下载图片配置失败" ,e);
}
//加<textarea>以解决IE下submit后没有执行回调success函数,这个是jquery easyui form的bug
return "<textarea>" + m + "</textarea>";
}
说明:"<textarea>" + m + "</textarea>";和produces = {"text/html;charset=UTF-8"}解决IE下不能执行回调函数success的问题
以上这篇form+iframe解决跨域上传文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
标签:
iframe,跨域解决办法
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“form+iframe解决跨域上传文件的方法”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2025年10月25日
2025年10月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]