五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > 讓IE瀏覽器打開JSON文件直接顯示不彈出下載提示的處理方法

讓IE瀏覽器打開JSON文件直接顯示不彈出下載提示的處理方法

文章來源:365jz.com     點擊數(shù):2570    更新時間:2018-03-18 16:48   參與評論
為了測試更方便,一般我使用 Web API 都會設(shè)置讓 Web API 返回 Json 格式。在IE瀏覽器中,當(dāng)伙同在地址輸入 URL 后 IE 瀏覽器會彈出是否需要下載的提示。
實際上 IE 彈出下載提示也沒有什么不好的,但是有時候想要迫不及待的看到Json返回的結(jié)果時,又需要反復(fù)的下載再打開查看,這個行為就顯得讓人討厭了。
如果能夠像 Chrome 或 Firefox 可直接看結(jié)果就好了。

在做一個接口,返回的是json文件,做測試為了方便直接在網(wǎng)頁上打開瀏覽。

解決辦法也非常簡單,需要我們在操作系統(tǒng)的注冊表中添加關(guān)于 JSON 的 MIME 類型支持,你可以將以下內(nèi)容編輯成擴展名為 .reg 的文件(文件名隨意),然后雙擊執(zhí)行將該文件導(dǎo)入注冊表:
Windows Registry Editor Version 5.00
 
[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"Encoding"=dword:00080000
 
[HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json]
"CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
"encoding"=dword:00080000
其原理是修改注冊表,將 application/json、text/json 兩種 Content-Type 開啟設(shè)置調(diào)成與 GIF/PNG/HTML 一致,改為直接用瀏覽器打開查看。 導(dǎo)入上面的注冊表文件以后,再使用IE開啟就可以不需要下載即可顯示 json 了。

問題:

用jquery.form.js異步提交表單時,接收的是JSON格式的數(shù)據(jù),但是奇怪的是在IE中提示下載文件,其他瀏覽器中一切正常,下載后,里面的內(nèi)容就是在控制器中返回的數(shù)據(jù)。

解決方案


原返回值設(shè)定: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轉(zhuǎn)換成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();                                       
                }
            }
        });

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答!! 點擊進入論壇

發(fā)表評論 (2570人查看0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號