我有一个页面显示根据所选选项生成的 MS 图表。
以前在本地进行时,我可以保存图表图像并通过 ExcelDl_Click 将其显示在 Excel 工作表中。但是在将系统部署到IIS之后,我得到的保存的“图表”图像是一个空白的方形图像。
这是在本地和部署中使用的代码:
protected void ExcelDl_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ViewHistoryChart.xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Chart1.RenderControl(hw);
string tmpChartName = "ChartImage.jpg";
string imgPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;
Chart1.SaveImage(imgPath);
string src = tmpChartName;
string img = string.Format("<img src = '{0}{1}' />", HttpContext.Current.Request.PhysicalApplicationPath, src);
Table table = new Table();
TableRow row = new TableRow();
Unit width = new Unit(500, UnitType.Pixel);
row.Cells.Add(new TableCell());
row.Cells[0].Width = width;
row.Cells[0].Controls.Add(new Label { Text = ChartTitle.Text, ForeColor=Color.Red, });
table.Rows.Add(row);
row = new TableRow();
row.Cells.Add(new TableCell());
row.Cells[0].Controls.Add(new Literal { Text = lblSectionFunctionSelected.Text });
table.Rows.Add(row);
row = new TableRow();
row.Cells.Add(new TableCell());
row.Cells[0].Controls.Add(new Literal { Text = img });
table.Rows.Add(row);
row.Cells.Add(new TableCell());
row.Cells[0].Controls.Add(new Literal { Text = lbllegendHistory.Text });
table.Rows.Add(row);
sw = new StringWriter();
hw = new HtmlTextWriter(sw);
table.RenderControl(hw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
}
当我单击页面上的 ChartImg.axd 图像链接时,图表显示正确。但是我点了ExcelDl_Click之后,再打开链接就什么都没有了。我觉得这与我保存图像的方式有关,但我不确定。是否需要任何软件包更新来解决此问题?任何帮助将不胜感激。
最佳答案
您遇到此问题的原因是 Web 应用程序无法保存到服务器的本地文件系统。因此,当您执行 Chart1.SaveImage(imgPath);
时,它实际上并没有保存。如果您要坚持使用此模式,应用程序池用户将需要对此文件夹的写入权限。
关于C# - MS 图表图像在保存后变为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35491298/