ASP.NET MVC 使用jquery.form.js 异步上传 在IE下返回值被变为下载的解决办法

错误记录:

<script type="text/javascript">

        $(function () {

            $(document).off("ajaxSend");

            //异步上传

            $("#Submit").click(function () {

                if ($("#selectFileButton").val() == "") {

                    return false;

                }

                $("#fileForm").ajaxSubmit({

                    success: function (msg, status) {

                        hideMask();

                        if (msg == "") {

                            msg = "上传成功!";

                            $("#selectFileButton").val("");

                        } else {

                            var regex = new RegExp("\"", "g");

                            msg = msg.replace(regex, "");

                            msg = msg.replace(/\|/g, "<br>");

                        }

                        document.getElementById('uploadMessage').innerHTML = msg;

                    }

                });

                showMask();

                return false; //不刷新页面

            });

        });

    </script>

  如上代码,在页面进行异步上传。在chrome浏览器下,返回的值会在页面中显示。但在IE8下(项目需要,只测试了IE8),返回值变为了下载。

 

  查询原因,最终定位到异步返回的Response Head上。

这里的Response Head中,Content-Type为application/json,在IE8下则会进行下载。

 

解决办法:

            var rs = Json(

                message = message

            );

            rs.ContentType = "text/html";

            return rs;        

  如上代码中的红色字体。

在异步返回的方法中,修改返回值的Response Head的Content-Type为text/html

你可能感兴趣的:(asp.net)