public ActionResult CustomChart(int reportID)
{
Chart chart = new Chart();
// Save the chart to a MemoryStream
var imgStream = new MemoryStream();
chart.SaveImage(imgStream);
imgStream.Seek(0, SeekOrigin.Begin);
// Return the contents of the Stream to the client
return File(imgStream, "image/png");
}
我習慣於將'using'語句與MemoryStreams結合使用。這是「使用」語句不必要的場景嗎?或者在'using'語句中調用return是否有效?MemoryStream是否在作爲ActionResult返回時自動處理?
編輯:
對於我而言,我發現,引入的「使用」語句不工作(拋出的ObjectDisposedException)。以下是我在客戶端做的事情:
$('#ReportTest').bind('load', function() {
$('#LoadingPanel').hide();
$(this).unbind('load');
}).bind('error', function() {
$('#LoadingPanel').hide();
$(this).unbind('error');
}).attr('src', '../../Chart/CustomChart?ReportID=' + settings.id);
它總是一個好主意,與實現IDisposable,類打交道你是不是相信ASP.NET是要清理時,使用「使用」聲明在你之後。 – 2012-01-10 17:54:24
什麼'文件(流,字符串)'做與流?通常創建流的對象也應該處理流。在這種情況下,您將負責處理該流。 – 2012-01-10 17:55:56
@GeorgeStocker會調用Dispose()方法嗎?我想在調用'return'之後,它不會完成'using'塊。 – 2012-01-10 17:59:34