解決在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.
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();
}
}
});