Ajax异步无刷新显示图片
前台:
//图片
var path = '<%= ResolveUrl("~/Img/") %>';
var imgUrl = path + "/" + result.FileName.substr(result.FileName.lastIndexOf('/') + 1);
var img = new Image();
img.src = imgUrl;
var path = '<%= ResolveUrl("~/Img/") %>';
var imgUrl = path + "/" + result.FileName.substr(result.FileName.lastIndexOf('/') + 1);
var img = new Image();
img.src = imgUrl;
if (!img.complete) {
//无图片
}
else //有图片
{
if (img.width < 500) { //小于最大宽度,按实际宽度显示
$("#<%=Image1.ClientID %>").css("width", img.width);
}
else { //大于等于最大宽度,按最大宽度显示
$("#<%=Image1.ClientID %>").css("width", 500);
}
if (img.width < 500) { //小于最大宽度,按实际宽度显示
$("#<%=Image1.ClientID %>").css("width", img.width);
}
else { //大于等于最大宽度,按最大宽度显示
$("#<%=Image1.ClientID %>").css("width", 500);
}
}
后台初始化获取第一张图片:
System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(Server.MapPath(url));
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
if (sourceWidth < 500)
{
Unit width = new Unit(sourceWidth);
this.Image1.Width = width;
}
if (sourceHeight < 350)
{
Unit height = new Unit(sourceHeight);
this.Image1.Height = height;
}
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
if (sourceWidth < 500)
{
Unit width = new Unit(sourceWidth);
this.Image1.Width = width;
}
if (sourceHeight < 350)
{
Unit height = new Unit(sourceHeight);
this.Image1.Height = height;
}
ashx文件获取切换的图片数据(使用json返回):
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonStr = serializer.Serialize(model);
context.Response.Write(jsonStr);
string jsonStr = serializer.Serialize(model);
context.Response.Write(jsonStr);