Files
lux/TestDevExpress/Components/Pages/CustomReportStorageWebExtension.cs
Annamaria Sassi 4b41ed6dde - Gestito ReportDesign
- Importazione ReportDesign in ReportViewer
2026-04-03 12:31:42 +02:00

141 lines
5.7 KiB
C#

using DevExpress.XtraReports.UI;
using System.ServiceModel;
using System.Web;
public class CustomReportStorageWebExtension : DevExpress.XtraReports.Web.Extensions.ReportStorageWebExtension
{
readonly string reportDirectory = "Reports";
const string FileExtension = ".repx";
public CustomReportStorageWebExtension()
{
if (!Directory.Exists(reportDirectory))
{
Directory.CreateDirectory(reportDirectory);
}
}
public override bool CanSetData(string url)
{
// Determines whether a report with the specified URL can be saved.
// Add custom logic that returns **false** for reports that should be read-only.
// Return **true** if no valdation is required.
// This method is called only for valid URLs (if the **IsValidUrl** method returns **true**).
return true;
}
public override bool IsValidUrl(string url)
{
// Determines whether the URL passed to the current report storage is valid.
// Implement your own logic to prohibit URLs that contain spaces or other specific characters.
// Return **true** if no validation is required.
return Path.GetFileName(url) == url;
}
public override byte[] GetData(string url)
{
// Uses a specified URL to return report layout data stored within a report storage medium.
// This method is called if the **IsValidUrl** method returns **true**.
// You can use the **GetData** method to process report parameters sent from the client
// if the parameters are included in the report URL's query string.
//try
//{
// if (Directory.EnumerateFiles(reportDirectory).Select(Path.GetFileNameWithoutExtension).Contains(url))
// {
// return File.ReadAllBytes(Path.Combine(reportDirectory, url + FileExtension));
// }
//}
//catch (Exception)
//{
// throw new FaultException(new FaultReason("Could not get report data."), new FaultCode("Server"), "GetData");
//}
//throw new FaultException(new FaultReason(string.Format("Could not find report '{0}'.", url)), new FaultCode("Server"), "GetData");
string ReportDirectory = "Reports";
try
{
// Parse the string with the report name and parameter values.
string[] parts = url.Split('?');
string reportName = parts[0];
string parametersQueryString = parts.Length > 1 ? parts[1] : String.Empty;
// Create a report instance.
XtraReport report = null;
if (Directory.EnumerateFiles(ReportDirectory).
Select(Path.GetFileNameWithoutExtension).Contains(reportName))
{
byte[] reportBytes = File.ReadAllBytes(Path.Combine(ReportDirectory, reportName + FileExtension));
using (MemoryStream ms = new MemoryStream(reportBytes))
report = XtraReport.FromStream(ms);
}
if (report != null)
{
// Apply the parameter values to the report.
var parameters = HttpUtility.ParseQueryString(parametersQueryString);
foreach (string parameterName in parameters.AllKeys)
{
report.Parameters[parameterName].Value = Convert.ChangeType(
parameters.Get(parameterName), report.Parameters[parameterName].Type);
}
// Disable the Visible property for all report parameters
// to hide the Parameters Panel in the viewer.
foreach (var parameter in report.Parameters)
{
parameter.Visible = false;
}
// If you do not hide the panel, disable the report's RequestParameters property.
// report.RequestParameters = false;
using (MemoryStream ms = new MemoryStream())
{
report.SaveLayoutToXml(ms);
return ms.ToArray();
}
}
}
catch (Exception ex)
{
throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
"Could not get report data.", ex);
}
throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
string.Format("Could not find report '{0}'.", url));
}
public override Dictionary<string, string> GetUrls()
{
// Returns a dictionary that contains the report names (URLs) and display names.
// The Report Designer uses this method to populate the Open Report and Save Report dialogs.
return Directory.GetFiles(reportDirectory, "*" + FileExtension)
.ToDictionary(x => Path.GetFileNameWithoutExtension(x));
}
public override void SetData(XtraReport report, string url)
{
// Saves the specified report to the report storage with the specified name
// (saves existing reports only).
var resolvedUrl = Path.GetFullPath(Path.Combine(reportDirectory, url + FileExtension));
if (!resolvedUrl.StartsWith(Path.GetFullPath(reportDirectory) + Path.DirectorySeparatorChar))
{
throw new FaultException("Invalid report name.");
}
report.SaveLayoutToXml(resolvedUrl);
}
public override string SetNewData(XtraReport report, string defaultUrl)
{
// Allows you to validate and correct the specified name (URL).
// This method also allows you to return the resulting name (URL),
// and to save your report to a storage. The method is called only for new reports.
SetData(report, defaultUrl);
return defaultUrl;
}
}