c# - 如何在 IIS 上设置 rdlc 报告的自定义位置?

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

虽然我可以在 Debug模式下创建 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");
}       

最佳答案

以下是我们如何从 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);
    }

关于c# - 如何在 IIS 上设置 rdlc 报告的自定义位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34239420/

相关文章:

javascript - IISNode 站点列出文件夹而不是启动 server.js

c# - 如何访问不在Resources文件夹根目录下的资源文件? (共享点 2010)

c# - 异步异常未被捕获或被吞噬

c# - NHibernate LINQ 计算查询中的组数

iis - 在 IIS 中为默认网站配置 SSL 后 https url 未打开

asp.net - IIS 如何知道它是为网站还是 Web 应用程序项目提供服务?

c# - CentOS 服务器上的 ASP .NET 核心控制台应用程序丢弃请求

javascript - ModelClientValidationRule ValidationType 字符串的有效值?

asp.net-mvc - EF 映射对象的不兼容数据读取器异常

asp.net-mvc - ASP.NET mvc 4 Controller 参数在将 json 发送到 Controller 时始终为空,为什么?