【问题标题】:How to set custom location for rdlc report on IIS?如何在 IIS 上设置 rdlc 报告的自定义位置?
【发布时间】:2015-12-12 11:42:43
【问题描述】:

虽然我可以在调试模式下创建 rdlc 报告,但遇到错误“拒绝访问路径 'C:\xxx.xlsx'。” 在网上查找解决方法后,我看到很多解决方案建议为 IIS 用户授予 C 驱动器权限。但是,授予整个驱动器仅用于呈现报告的权限似乎并不明智。那么,如何更改此渲染位置,即 C:\inetpub\MyApplication?另一方面,我认为报告方面不需要设置,即ReportViewer.ProcessingMode = ProcessingMode.Local; 或更改 rdlc 属性“构建操作”“复制输出目录”

注意:我不希望报告在客户端机器上呈现,因为其中一些无权在 C:\ 下写入任何位置,我认为在 IIS 位置生成报告要好得多.不是吗?

那么,在这种情况下,最好的解决方案是什么?


更新:我怎样才能修改这个方法,使它只读取流作为 excel 而无需编写它?

public static void StreamToProcess(Stream readStream)
{
    var writeStream = new FileStream(String.Format("{0}\\{1}.{2}", Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "MyFile", "xlsx"), FileMode.Create, FileAccess.Write);
    const int length = 16384;
    var buffer = new Byte[length];
    var bytesRead = readStream.Read(buffer, 0, length);
    while (bytesRead > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
        bytesRead = readStream.Read(buffer, 0, length);
    }
    readStream.Close();
    writeStream.Close();
    Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + "\\" + "file" + "." + "xlsx");
}       

【问题讨论】:

  • 为什么要渲染到文件?只需从 MemoryStream 返回一个 FileStreamResult 并让浏览器处理它。
  • 你能举个例子吗?

标签: c# asp.net-mvc iis rdlc microsoft-reporting


【解决方案1】:

这是我们如何从 rdlc 渲染 Excel 文件而不将其保存到服务器文件夹。只需调用该操作,它就会下载到用户的浏览器。

    public FileStreamResult ExcelReport(int type)
    {

        var body = _db.MyObjects.Where(x => x.Type == type);
        ReportDataSource rdsBody = new ReportDataSource("MyReport", body);
        ReportViewer viewer = new ReportViewer 
        { 
            ProcessingMode = ProcessingMode.Local 
        };
        viewer.LocalReport.ReportPath = Server.MapPath(@"~\bin\MyReport.rdlc");
        viewer.LocalReport.DataSources.Clear();
        viewer.LocalReport.DataSources.Add(rdsBody);
        viewer.LocalReport.EnableHyperlinks = true;
        string filename = string.Format("MyReport_{0}.xls", type); 
        byte[] bytes = viewer.LocalReport.Render("Excel");
        var stream = new MemoryStream(bytes);
        return File(stream, "application/ms-excel", filename);
    }

【讨论】:

  • 非常感谢您的回复。我添加了问题的更新。那么,我怎样才能发出上面更新中提到的一点通知呢?
  • 我可能误解了你想要什么。此答案适用于从 rdlc 到客户端的 Excel 输出。您上面的代码看起来像是在尝试让 Excel 在客户端上运行。由于明显的安全问题,我认为您不能这样做。我们制作了一个 xls 文件,用户可以点击它来启动 Excel。
  • 对不起,我有点困惑。我只是想确定我们是否应该通过删除磁盘的“写入”部分来修改这种“文件生成”或“报告生成”方法(我认为问题是由于尝试写入磁盘而我们需要内存来生成和显示文件)。这是真的吗?
  • 你可以做任何一个,但是如果你写一个文件会有安全问题,而内存流是由浏览器处理的。它仍然将其保存为文件。最好的建议是尝试流式传输方法,看看它是否满足您的需求。
  • 我会尽快尝试的。但即使它不能解决我的问题,也非常感谢您的帮助。投票+
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
相关资源
最近更新 更多