gpt4 book ai didi

java - IE 正在将 Controller 的响应作为文件

转载 作者:行者123 更新时间:2023-12-01 13:06:39 24 4
gpt4 key购买 nike

以下是我尝试从我的代码上传文件的代码,代码在 chrome 和 mozilla 中正常工作,但不适用于 IE。对于 IE,当 Controller 返回 int (显示文件是否上传成功)时,它给出浏览器作为文件,因此它要求打开或保存文件,如果我在记事本中打开该文件,它会显示 int 为 1 或 0,具体取决于结果,我不知道为什么它将它作为文件。请帮助我.

// controller side code
@RequestMapping(value = "upload", method=RequestMethod.POST)
//@produces(text/html) //tried this as well but didnt change the result.
public @ResponseBody int upload(HttpServletRequest request, HttpServletResponse response,
@RequestPart("dataFile") MultipartFile file
){ code to write file on mentioned directory which returns 1 on success and 0 on failure}

//javascript 代码,将 iframe(提交表单)提交给 Controller

<html>

<script language="Javascript">
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");

// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";

iframeId = document.getElementById("upload_iframe");

// Add event...
var eventHandler = function () {

if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);

// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}

document.getElementById(div_id).innerHTML = content;

// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}

if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);

// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");

// Submit the form...
form.submit();

document.getElementById(div_id).innerHTML = "Uploading...";
}
</script>

//html code
<!-- index.php could be any script server-side for receive uploads. -->
<form>
<input type="file" name="dataFile" id="dataFile" /></br>
<input type="button" value="upload"
onClick="fileUpload(this.form,'upload','uploadShow'); return false;" >
<div id="uploadShow"></div>
</form>

</html>


//code of view resolver
public class AppConfig {

// Resolve logical view names to .jsp resources in the /WEB-INF/views directory
@Bean
ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

}

最佳答案

抱歉回复晚了。在尝试您的代码之前,我必须先设置 spring mvc 环境。

我已将 Controller 方法的返回类型从 int 更改为 String,显然它现在可以工作了。你可以尝试一下吗?

public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response,
@RequestPart("dataFile") MultipartFile file ){
if(file!=null){
return "1";
}
return "0";
}

关于java - IE 正在将 Controller 的响应作为文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218842/

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