解决在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("<img src=\"" + urlPath + "Temp/" + item.tempfilename + "\" id=\"TempImage\" autocomplete=\"off\" />");                        
                        $("#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();                                       
                }
            }
        });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值