2012-02-13 35 views
0

我使用下面的代碼使用jQuery重新加載圖像:使用控制器IE下未能更改圖像的src一旦SRC已經定義一旦

//Fetch image associated with new image imap. 
var reportID = parseInt($('#ReportSelector').val()); 

$('#HistoricalChart').bind('load', function() { 
    alert("loaded"); 
    $(this).unbind('load'); 
    $('#ChartLoading').hide(); 
    $(this).show(); 
}).bind('error', function() { 
    $(this).unbind('error'); 
    $('#ChartLoading').hide(); 
    $('#ChartLoadingError').show(); 
}).attr('src', '../Chart/HistoricalChart?ReportID=' + reportID); 

這個jQuery鏈的最後一行表示我希望改變HistoricalChart的src屬性。我使用以下控制器操作來執行此操作:

public ActionResult HistoricalChart(int reportID) 
{ 
    LineChart lineChart = (LineChart)Session[string.Format("HistoricalReport: {0}", reportID)]; 
    MemoryStream memoryStream = lineChart.ConvertToMemoryStream(); 
    return File(memoryStream, "image/png"); 
} 

這可以在Chrome下正常工作。然而,在IE9下,這只是第一次。每次在IE下都會觸發「加載」警報 - 只有圖像不會更改。

也就是說,我有一個斷點HistoricalChart方法的內部設置。 HistoricalChart img第一次設置了src - 斷點被擊中。然而,在這之後,調用.attr('src',...)後斷點不會被擊中。

理想的情況下我也不會清除src屬性,或隱藏圖像,來解決這個問題。與其交互時,我不希望圖像閃爍。

這是IE的一個已知限制嗎?解決方法?或者我在做一些不正確的事情?

+0

它是一個緩存的問題?嘗試在您的網址中添加緩存攔截器。 '../Chart/HistoricalChart?ReportID='+ reportID +'&cachebuster ='+ new Date()。getTime()' – 2012-02-13 18:23:37

回答

1

可能是您第二次獲取緩存圖像。嘗試添加一個隨機數到源網址。

$('#HistoricalChart').bind('load', function() { 
    alert("loaded"); 
    $(this).unbind('load'); 
    $('#ChartLoading').hide(); 
    $(this).show(); 
}).bind('error', function() { 
    $(this).unbind('error'); 
    $('#ChartLoading').hide(); 
    $('#ChartLoadingError').show(); 
}).attr('src', '../Chart/HistoricalChart?ReportID=' + reportID 
                + "&r=" + Math.random()); 
+0

除了'Math.random()'可能會返回相同的值。對於緩存清除值,最好使用'new Date()。getTime()' – 2012-02-13 18:25:27

+0

這解決了這個問題。謝謝。我會在7分鐘內接受。 :) – 2012-02-13 18:26:28

+0

@JuanMendes指出 - 雖然是一個非常小的邊緣情況下,邊緣情況下。我將使用Date()。getTime() – 2012-02-13 18:27:17