Ajax異步無刷新顯示圖片


前台:

 

// 圖片
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);
     }
}

后台初始化獲取第一張圖片:

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;
}

ashx文件獲取切換的圖片數據(使用json返回):

 

JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonStr = serializer.Serialize(model);
 
context.Response.Write(jsonStr);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM