penley 发表于 2020-11-30 20:56:25

JSViewer如何定位子文件夹中的报表文件?

本帖最后由 penley 于 2020-12-1 19:48 编辑

环境:AR14、.net core mvc项目、JSViewer。

一、项目根目录创建Reports文件夹,用于保存所有报表文件,计划不同的报表文件放在此文件夹下的不同子文件夹中(按模块分)。
比如,有一个“演示报表.rdlx”的路径如下:
Reports/ReportDemo/演示报表.rdlx并且,所有报表文件属性设置为“始终复制”,保证可以随应用一起发布。

二、MVC项目的Startup.cs中,设置报表文件根为Reports文件夹。
            settings.UseFileStore(new DirectoryInfo("Reports"));

三、在JSViewer中加载报表
var viewer = GrapeCity.ActiveReports.JSViewer.create({
      element: '#viewerContainer',
      reportID: 'ReportDemo/演示报表.rdlx'
});

如果使用MVC项目直接启动, 是可以找到报表文件的,一切正常。但使用IIS Express启动项目,就无法找到报表。
类似如下:


注:如果把报表文件直接放在Reports文件夹下(无子文件夹),reportID指定为“演示报表.rdlx”,都是正常的。

四、尝试指定不同的路径,均以失败告终
1、演示报表.rdlx
2、Reports/ReportDemo/演示报表.rdlx
3、/Reports/ReportDemo/演示报表.rdlx
4、ReportDemo\\演示报表.rdlx
5、\\ReportDemo\\演示报表.rdlx
6、Reports\\ReportDemo\\演示报表.rdlx
7、\\Reports\\ReportDemo\\演示报表.rdlx

问题:
reportID 参数到底需要传个什么样的格式?谢谢!!

KearneyKang 发表于 2020-12-1 10:25:07

你好, 个人建议ReportID写成这样的reportID: '演示报表.rdlx'这种格式是通用的,然后在后端Startup的方法中进行相对路径的匹配
你可以看看这个写法
using System;
using System.Data;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using System.Web.Routing;
using System.Windows.Media.Imaging;
using GrapeCity.ActiveReports;
using GrapeCity.ActiveReports.Aspnet.Viewer;
using Microsoft.Owin;
using Owin;



namespace JSviewerDatasource
{
    public class Startup1
    {
      public static string EmbeddedReportsPrefix = "JSdatasource";
      string ReportName = "";
      public object GetReport(string P)//获取报表名称和报表参数,进行一个对应的报表名称和参数的分割
      {
            //string reportName = P.Split(';');//报表名称;
            //string parameter1 = P.Split(';');//参数1;
            //string parameter2 = P.Split(';');//参数2;
            string reportName = P;//报表名称;
            PageReport rep = new PageReport();
            string path = System.Web.Hosting.HostingEnvironment.MapPath("~/");
            rep.Load(new FileInfo(@"" + path + "Reports/" + reportName));         
            return rep.Report;
         
      }

      public void Configuration(IAppBuilder app)
      {
      
            app.UseReporting(settings =>
            {
                settings.UseCompression = true;
                settings.UseCustomStore(GetReport);//使用UseCustomStore来自定义一些需要的值
                //settings.UseFileStore(new DirectoryInfo(String.Format(@"{0}.\Reports\", HttpRuntime.AppDomainAppPath)));               
                settings.LocateDataSource = args =>
                {
                  DataTable dt = new DataTable();
                  string name = ReportName;
                  //var p = args.Parameters.Value.ToString();
                  if (args.DataSet.Query.DataSourceName == "DataSource1")
                  {                     
                        if (args.DataSet.Name == "DataSet1")
                        {
                            dt.Columns.Add("产品编号");
                            dt.Columns.Add("产品名称");
                            dt.Columns.Add("单价");
                            dt.Columns.Add("库存量");
                            dt.Columns.Add("产地");
                            dt.Rows.Add("A001", "苹果", 10, 300, "中国");
                            dt.Rows.Add("A002", "葡萄", 20, 200, "中国");
                            dt.Rows.Add("A003", "香蕉", 30, 400, "中国");
                            dt.Rows.Add("A004", "甘蔗", 10, 300, "中国");
                            dt.Rows.Add("A005", "荔枝", 20, 200, "中国");
                            dt.Rows.Add("A006", "芒果", 30, 400, "中国");
                            dt.Rows.Add("A007", "猕猴桃", 110, 300, "中国");
                            dt.Rows.Add("A008", "柠檬", 210, 200, "中国");
                            dt.Rows.Add("A009", "栗子", 320, 400, "中国");
                            dt.Rows.Add("A010", "火龙果", 100, 300, "中国");
                            dt.Rows.Add("A011", "青芒", 250, 200, "中国");
                            dt.Rows.Add("A012", "巴旦木", 320, 200, "中国");
                            dt.Rows.Add("A013", "土豆", 380, 400, "中国");
                            dt.Rows.Add("A014", "苹果", 110, 300, "中国");
                            dt.Rows.Add("A015", "葡萄", 420, 200, "中国");
                            dt.Rows.Add("A016", "香蕉", 530, 400, "中国");
                            dt.Rows.Add("A017", "土豆", 380, 400, "中国");
                            dt.Rows.Add("A018", "苹果", 110, 300, "中国");
                            dt.Rows.Add("A019", "葡萄", 420, 200, "中国");
                            dt.Rows.Add("A020", "香蕉", 530, 400, "中国");
                            dt.Rows.Add("A021", "苹果", 10, 300, "中国");
                            dt.Rows.Add("A022", "葡萄", 20, 200, "中国");
                            dt.Rows.Add("A023", "香蕉", 30, 400, "中国");
                            dt.Rows.Add("A024", "甘蔗", 10, 300, "中国");
                            dt.Rows.Add("A025", "荔枝", 20, 200, "中国");
                            dt.Rows.Add("A026", "芒果", 30, 400, "中国");
                            dt.Rows.Add("A001", "苹果", 10, 300, "中国");
                            dt.Rows.Add("A002", "葡萄", 20, 200, "中国");
                            dt.Rows.Add("A003", "香蕉", 30, 400, "中国");
                            dt.Rows.Add("A004", "甘蔗", 10, 300, "中国");
                            dt.Rows.Add("A005", "荔枝", 20, 200, "中国");
                            dt.Rows.Add("A006", "芒果", 30, 400, "中国");
                            dt.Rows.Add("A007", "猕猴桃", 110, 300, "中国");
                            dt.Rows.Add("A008", "柠檬", 210, 200, "中国");
                            dt.Rows.Add("A009", "栗子", 320, 400, "中国");
                            dt.Rows.Add("A010", "火龙果", 100, 300, "中国");
                            dt.Rows.Add("A011", "青芒", 250, 200, "中国");
                            dt.Rows.Add("A012", "巴旦木", 320, 200, "中国");
                            dt.Rows.Add("A013", "土豆", 380, 400, "中国");
                            dt.Rows.Add("A014", "苹果", 110, 300, "中国");
                            dt.Rows.Add("A015", "葡萄", 420, 200, "中国");
                            dt.Rows.Add("A016", "香蕉", 530, 400, "中国");
                            dt.Rows.Add("A017", "土豆", 380, 400, "中国");
                            dt.Rows.Add("A018", "苹果", 110, 300, "中国");
                            dt.Rows.Add("A019", "葡萄", 420, 200, "中国");
                            dt.Rows.Add("A020", "香蕉", 530, 400, "中国");
                            dt.Rows.Add("A021", "苹果", 10, 300, "中国");
                            dt.Rows.Add("A022", "葡萄", 20, 200, "中国");
                            dt.Rows.Add("A023", "香蕉", 30, 400, "中国");
                            dt.Rows.Add("A024", "甘蔗", 10, 300, "中国");
                            dt.Rows.Add("A025", "荔枝", 20, 200, "中国");
                            dt.Rows.Add("A026", "芒果", 30, 400, "中国");
                        }                     
                  }               
                  return dt;
                };


            });

      }      
    }
}


penley 发表于 2020-12-1 19:48:12

好的,谢谢!我试试。:)

KearneyKang 发表于 2020-12-1 20:00:52

好的,你试试,有问题随时沟通
页: [1]
查看完整版本: JSViewer如何定位子文件夹中的报表文件?