IE正在将控制器的响应作为文件

时间:2014-04-22 11:58:18

标签: java javascript spring iframe

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

// 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(提交表单)提交给控制器

<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;
    }

}

1 个答案:

答案 0 :(得分:1)

很抱歉迟到的回复。在尝试代码之前,我必须首先设置spring mvc环境。

我已经将控制器方法的返回类型从int更改为String,显然它现在可以正常工作。你能试一试。

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