其原理是修改注册表,将 application/json、text/json 两种 Content-Type 开启设置调成与 GIF/PNG/HTML 一致,改为直接用浏览器打开查看。 导入上面的注册表文件以后,再使用IE开启就可以不需要下载即可显示 json 了。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
问题:
用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();
}
}
});
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛