gpt4 book ai didi

javascript - 为什么 IE 试图下载文件,如何解决?

转载 作者:行者123 更新时间:2023-11-30 06:29:55 24 4
gpt4 key购买 nike

我有一个简单的代码,用于上传图像文件并通过隐藏的 iframe 和来自服务器的 json 响应显示它。它在 chrome 和 firefox 中工作正常,但不确定 IE 导致文件下载和不显示图像的问题是什么。它正在尝试下载一些 239917890.json 文件。在保存文件时,我可以看到内容为

{"status":"success","id":2,"path":"enoem/manage_logos/show_logo/2","errorMsg":null}

java代码

@ResponseBody
@RequestMapping(value = "/manage_logos/upload_logo/{Id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public EnoemLogoJson uploadLogo(@PathVariable Integer Id, @RequestParam("file") MultipartFile file,
Model model, HttpServletResponse response) throws IOException {
EnoemLogoJson enoemLogoJson = new EnoemLogoJson();
EnoemConfiguration enoemConfiguration = enoemConfigurationRepository.findById(Id);
if (validateLogo(file)) {
enoemLogoJson.setErrorMsg("Error");
return enoemLogoJson;
}
LOGGER.info("File name is " + file.getOriginalFilename());
enoemConfiguration.setLogo(file.getBytes());
enoemConfigurationRepository.saveAndFlush(enoemConfiguration);
enoemLogoJson.setStatus("success");
enoemLogoJson.setId(Id);
enoemLogoJson.setPath("enoem/manage_logos/show_logo/" + Id);
return enoemLogoJson;
}

html代码

<form id="file_upload_form" method="post" enctype="multipart/form-data" action="enoem/manage_logos/upload_logo/1">
<div class="row">
<input name="file" id="file" size="27" type="file">
</div>
<div class="btn-row">
<input type="submit" name="action" class="cButton upload-logo-btn" value="Upload">
<input id="close-upload-popup" type="button" value="Cancel" class="cButton cancel-btn-table">
</div>
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>

JavaScript

var $form = $('#file_upload_form');
$form.unbind('submit');
$form.bind('submit', function(a,b,c){
this.target = 'upload_target';
alert('Firing in all browsers');
$('#upload_target').load(function(d,e,f){
alert('When file uploaded'); //Not firing in IE but IE is trying to download some data
var data = $('#upload_target').contents().find('pre').html();
data = $.parseJSON(data);
if(data){
//Here doing showing information from data object
//working fine in chrome/firefox but not in IE
}
});
});

问题出在 IE9 和 IE10 上。 (IE8未测试)

最佳答案

请引用您返回的json格式。 http://json.parser.online.fr/

返回如下格式的json。"{success:"+ result + ",message:'"+ responseText + "'}

关于javascript - 为什么 IE 试图下载文件,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18355191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com