原創(chuàng)|使用教程|編輯:鄭恭琳|2017-11-21 17:46:39.000|閱讀 620 次
概述:Online Designer用戶面臨的首要問題之一是如何組織從本地計(jì)算機(jī)下載報(bào)告? 今天,我們將考慮從本地計(jì)算機(jī)上傳到Online Designer,并使用ASP.Net MVC應(yīng)用程序的示例下載已修改的報(bào)告。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Online Designer用戶面臨的首要問題之一是如何組織從本地計(jì)算機(jī)下載報(bào)告? 今天,我們將考慮從本地計(jì)算機(jī)上傳到Online Designer,并使用ASP.Net MVC應(yīng)用程序的示例下載已修改的報(bào)告。
創(chuàng)建一個ASP.Net MVC項(xiàng)目。 我們將需要以下庫:
打開控制器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。 不過,首先我們要創(chuàng)建一個Web報(bào)表對象和一個用于存儲報(bào)表文件的緩存。 我使用緩存是為了避免將文件保存在服務(wù)器上:
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(); }
獲取報(bào)告路徑的方法:
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)參數(shù)。 它指定緩存的有效期。
現(xiàn)在我們需要一個在線設(shè)計(jì)器中保存報(bào)告的方法:
[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(); }
我們?yōu)檫@個方法創(chuàng)建一個單獨(dú)的視圖SaveDesignedReport.cshtml:
@ViewBag.Message
它仍然是實(shí)現(xiàn)下載報(bào)告文件的方法:
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"); }
現(xiàn)在考慮索引頁面的視圖(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()
在頂部我們顯示頁面的標(biāo)題。 接下來,使用BeginForm助手來創(chuàng)建一個帶有文件選擇按鈕的表單。 參數(shù)指定處理器方法的名稱 - “Upload”,控制器名稱為“Home”,處理方法為FormMethod.Post,數(shù)據(jù)編碼方式為 - enctype =“multipart / form-data”。
接下來,插入文件下載字段和按鈕。
在頁面的右側(cè),我們將放置一個按鈕,用其上傳/下載編輯后的報(bào)告。 對于它,我們也使用BeginForm助手創(chuàng)建一個表單。
在最后一行代碼中,我們顯示從控制器收到的報(bào)告。
有必要連接文件_Layout.cshtml中的腳本:
@WebReportGlobals.Scripts() @WebReportGlobals.Styles()
現(xiàn)在您需要對兩個Web配置進(jìn)行更改。 這些文件被稱為相同,但它們位于不同的文件夾中。 第一個位于Views文件夾中。 我們加入:
…
第二個文件位于項(xiàng)目的根目錄下。 在其中我們添加一個處理程序:
…
運(yùn)行我們的應(yīng)用。
我們看到一個空的報(bào)告OnlineDesigner。 使用“選擇文件”按鈕從本地計(jì)算機(jī)下載報(bào)告。 從對話框中選擇文件并點(diǎn)擊上傳按鈕:
報(bào)告模板已加載。 讓我們改變數(shù)據(jù)帶中的背景顏色。 在“報(bào)告”標(biāo)簽上,我們點(diǎn)擊“保存”按鈕:
SaveDesignedReport方法起作用,我們看到右邊的綠色警報(bào):
現(xiàn)在點(diǎn)擊“Download designed report”按鈕:
瀏覽器下載我們的報(bào)告 使用報(bào)表設(shè)計(jì)器打開它:
這樣就得到了我們的報(bào)告,接下來可以用Online Designer進(jìn)行編輯。
產(chǎn)品介紹 | 下載試用 | 優(yōu)惠活動 | | 聯(lián)系Elyn
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn