原創|使用教程|編輯:鄭恭琳|2017-11-21 17:46:39.000|閱讀 620 次
概述:Online Designer用戶面臨的首要問題之一是如何組織從本地計算機下載報告? 今天,我們將考慮從本地計算機上傳到Online Designer,并使用ASP.Net MVC應用程序的示例下載已修改的報告。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Online Designer用戶面臨的首要問題之一是如何組織從本地計算機下載報告? 今天,我們將考慮從本地計算機上傳到Online Designer,并使用ASP.Net MVC應用程序的示例下載已修改的報告。
創建一個ASP.Net MVC項目。 我們將需要以下庫:
打開控制器HomeController.cs。 將缺少的庫添加到uses部分:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.UI; using System.Runtime.Caching; using System.Text; using System.IO; using FastReport; using FastReport.Web; using FastReport.Utils; using System.Web.UI.WebControls; using FastReport.Export.Html; using FastReport.Data; using System.Net.Http.Headers; using FastReport.Export.Image; using System.Net.Http;
我們將在Index方法中顯示OnlineDesigner。 不過,首先我們要創建一個Web報表對象和一個用于存儲報表文件的緩存。 我使用緩存是為了避免將文件保存在服務器上:
private WebReport webReport = new WebReport(); //Report object MemoryCache cache = MemoryCache.Default; //Cache public ActionResult Index(HttpPostedFileBase upload) { webReport.Width = Unit.Percentage(100); webReport.Height = Unit.Percentage(100); string report_path = GetReportPath(); // The path to the folder with reports System.Data.DataSet dataSet = new System.Data.DataSet(); dataSet.ReadXml(report_path + "nwind.xml"); //Read database webReport.Report.RegisterData(dataSet, "NorthWind"); // Register the data in the report // If you do not use the cache, then load the report from the server if (System.IO.File.Exists(report_path + "report.frx")) { webReport.Report.Load(report_path + "report.frx"); } // If you are using a cache, then load a report from it if (cache.Contains("1")) { webReport.Report.Load(cache["1"] as Stream); } // Online-Designer settings webReport.DesignReport = true; webReport.DesignScriptCode = false; webReport.Debug = true; webReport.DesignerPath = "~/WebReportDesigner/index.html"; webReport.DesignerSaveCallBack = "~/Home/SaveDesignedReport"; webReport.ID = "DesignReport"; ViewBag.WebReport = webReport; //Pass the report to View return View(); }
獲取報告路徑的方法:
private string GetReportPath() { return this.Server.MapPath("~/App_Data/"); }
接下來,我們添加上傳文件的方法:
[HttpPost] // Attribute indicates that the method is processing the Post request public ActionResult Upload(HttpPostedFileBase upload) { if (upload != null) { // Get file name string fileName = System.IO.Path.GetFileName(upload.FileName); // Save report in cache cache.Add("1", upload.InputStream, DateTimeOffset.Now.AddMinutes(1)); // If you save to a file on the server upload.SaveAs(Server.MapPath("~/App_Data/report.frx")); } return RedirectToAction("Index"); }
請注意DateTimeOffset.Now.AddMinutes(1)參數。 它指定緩存的有效期。
現在我們需要一個在線設計器中保存報告的方法:
[HttpPost] // call-back for save the designed report public ActionResult SaveDesignedReport(string reportID, string reportUUID) { ViewBag.Message = String.Format("Confirmed {0} {1}", reportID, reportUUID); if (reportID == "DesignReport") { //Save report in cache cache.Set("1", Request.InputStream, DateTimeOffset.Now.AddMinutes(10)); // If the report is saved to the server /*************************************/ Stream reportForSave = Request.InputStream; string pathToSave = Server.MapPath("~/App_Data/DesignedReports/test.frx"); using (FileStream file = new FileStream(pathToSave, FileMode.Create)) { reportForSave.CopyTo(file); } /*************************************/ } return View(); }
我們為這個方法創建一個單獨的視圖SaveDesignedReport.cshtml:
@ViewBag.Message
它仍然是實現下載報告文件的方法:
public FileResult GetFile() { Stream str = cache["1"] as Stream; // Prepare a file for download from the cache return File(str, "application/octet-stream","test.frx"); // If you used saving report to the file on the server return File(Server.MapPath("~/App_Data/DesignedReports/test.frx"), "application/octet-stream", "test.frx"); }
現在考慮索引頁面的視圖(Home-> Index.cshtml):
@{ ViewBag.Title = "Home Page"; }Select file
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { }@using (Html.BeginForm("GetFile", "Home", FormMethod.Get)) { }@ViewBag.WebReport.GetHtml()
在頂部我們顯示頁面的標題。 接下來,使用BeginForm助手來創建一個帶有文件選擇按鈕的表單。 參數指定處理器方法的名稱 - “Upload”,控制器名稱為“Home”,處理方法為FormMethod.Post,數據編碼方式為 - enctype =“multipart / form-data”。
接下來,插入文件下載字段和按鈕。
在頁面的右側,我們將放置一個按鈕,用其上傳/下載編輯后的報告。 對于它,我們也使用BeginForm助手創建一個表單。
在最后一行代碼中,我們顯示從控制器收到的報告。
有必要連接文件_Layout.cshtml中的腳本:
@WebReportGlobals.Scripts() @WebReportGlobals.Styles()
現在您需要對兩個Web配置進行更改。 這些文件被稱為相同,但它們位于不同的文件夾中。 第一個位于Views文件夾中。 我們加入:
…
第二個文件位于項目的根目錄下。 在其中我們添加一個處理程序:
…
運行我們的應用。
我們看到一個空的報告OnlineDesigner。 使用“選擇文件”按鈕從本地計算機下載報告。 從對話框中選擇文件并點擊上傳按鈕:
報告模板已加載。 讓我們改變數據帶中的背景顏色。 在“報告”標簽上,我們點擊“保存”按鈕:
SaveDesignedReport方法起作用,我們看到右邊的綠色警報:
現在點擊“Download designed report”按鈕:
瀏覽器下載我們的報告 使用報表設計器打開它:
這樣就得到了我們的報告,接下來可以用Online Designer進行編輯。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn