解决在IE浏览器中返回json格式的数据时提示下载办法

转载:http://www.111cn.net/wy/js-ajax/76891.htm

 

发现一个其怪的问题生成的json数据格式的文件在ie浏览器会自动下载json格式文件了,那么这种问题如何来解决下面一起来看看在IE浏览器中返回json格式的数据时提示下载问题.
 

问题:

用jquery.form.js异步提交表单时,接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据。

解决方案


原返回值设定:context.Response.ContentType = "application/json";

尝试: context.Response.ContentType = "text/xml;"; 失败

后来试了下:context.Response.ContentType = "text/plain;charset=UTF-8";


例子

public ActionResult ShangChuanEmpPhoto()
        {
            WebImage pic = GetImageFromRequest();
            //WebImage pic = new WebImage(Request.InputStream);
            int MaxWidth = 600;
            int MaxHeight = 400;
            int MinWidth = 160;
            int MinHeight = 160;
            if (pic != null)
            {
                //string imageName = getTalentId();
                string imageName = MyClasses.KwstuLib.ClassesLib.GetIdByTime();
                string imageFormat = pic.ImageFormat;
                if (pic.Width > MaxWidth || pic.Height > MaxHeight) pic.Resize(MaxWidth, MaxHeight, true, true);
                if (pic.Width < MinWidth || pic.Height < MinHeight) pic.Resize(MinWidth, MinHeight, true, false);
                pic.Save(getUploadPhotoPath() + "Temp\\" + imageName);
                TouXiangInfo touxiang = new TouXiangInfo()
                {
                    w_full = pic.Width,
                    h_full = pic.Height,
                    tempfilename = imageName + "." + imageFormat
                };

//返回必须是“text/html”,不然IE会提示下载
                return Json(new
                {                    
                    List = touxiang
                }, "text/html");
            }
            return Content("0");
        }


视图中:


//上传照片               
        $('#uploadfile').change(function () {            
            var filepath = $("#uploadfile").val();
            if (filepath == "") return false;
            var extStart = filepath.lastIndexOf(".");
            var ext = filepath.substring(extStart, filepath.length).toUpperCase();
            if (ext != ".BMP" && ext != ".PNG" && ext != ".GIF" && ext != ".JPG" && ext != ".JPEG") {
                alert("图片限于bmp,png,gif,jpeg,jpg格式");
                return false;
            }
            $('#ShangChuanTuPianForm').submit();            
        });
        $("#ShangChuanTuPianForm").ajaxForm({
            beforeSend: function () {
                $("#shangchuan").attr("disabled", "disabled").val("正在上传");                
            },

//默认类型必须是‘text/html’
            dataType: 'html',//默认就是html类型,不写对火狐有影响            
            cache: false,
            success: function (response) {
                if (response != "0") {
                    responseText = JSON.parse(response); //把html转换成json类型                    
                    $.each(responseText, function (index, item) {                                   
                        $("#TuPianImg").append("");                        
                        $("#tempfilename").val(item.tempfilename);
                        $("#w_full").val(item.w_full);
                        $("#h_full").val(item.h_full);
                    });                    
                    $("#TempImage").Jcrop({
                        //aspectRatio: 0.7692,
                        aspectRatio: 1,
                        setSelect: [0, 0, 160, 160],
                        onChange: changeCropInfo
                    }, function () {
                        JcropAPI = this;
                    });
                    $("#ShangChuanTuPianKongJian").hide();
                    $("#CaiJianTuPianKongJian").show();                                       
                }
            }
        });

转载于:https://www.cnblogs.com/lhlong/p/5951858.html

你可能感兴趣的:(解决在IE浏览器中返回json格式的数据时提示下载办法)