如何将文件写入用户的本地文件?

时间:2017-01-09 22:01:40

标签: asp.net-mvc asp.net-mvc-4 ext.net

我们的Intranet应用程序使用EXT.NET。 单击“视图”菜单时,当前它在EXT.NET窗口中显示PDF文件的内容。

我想将PDF文件的内容写入用户的本地文件,以便客户端的JavaScript函数可以使用window.open在新窗口中打开文件。

我认为控制器需要将FileStreamResult返回给View,然后View将FileStreamResult写入用户的本地文件。 我怎样才能做到这一点 ?谢谢。

这是从视图(Index.cshtml):

@{
ViewBag.Title = "Archive";
var X = Html.X();
}
@section SPAViews {
@(
X.Viewport().Layout(LayoutType.Fit).Items(
X.TabPanel().ID("ArchiveTabPanel")
.Items(
X.GridPanel().ID("GridPanel1").MarginSpec("1 1 1 1").Cls("cust-grid").Title("Archive").Icon(Icon.ServerCompressed)
    .Plugins(X.GridFilters())
    .View(Html.X().GridView().StripeRows(true).TrackOver(false))
    .SelectionModel(X.CheckboxSelectionModel().Mode(SelectionMode.Multi))
    .TopBar(
        X.Toolbar().MinHeight(35)
            .Items(
        :
                        , X.Button()
                                .Text("View")
                                .Icon(Icon.PageWhiteAcrobat)
                                    .DirectEvents(de =>
                                    {
                                        de.Click.Action = "Submit";
                                        de.Click.EventMask.ShowMask = true;
                                        de.Click.Method = HttpMethod.POST;
                                        de.Click.ExtraParams.Add(new Parameter()
                                        {
                                            Name = "selection",
                                            Value = "getselection()",
                                            Mode = ParameterMode.Raw
                                        });
                                        de.Click.Timeout = 360000;
                                    })

这是来自控制器:

public ActionResult Submit(string selection)
{
        int cnt = SetTempData(selection);
        RenderWindows(String.Format("View Archive {0}.",cnt),0,cnt);
        return this.Direct();
}
[ChildActionOnly]
public void RenderWindows(string title,int download,int cnt)
{
    Window win = new Window
    {
        ID = "Windows1",
        Title = title,
        Icon = Ext.Net.Icon.PageWhiteAcrobat,
        Height = download == 1 ? 150 : 600,
        Width = download == 1 ? 400 : 800,
        BodyPadding = 2,
        Modal = true,
        Layout = "Fit",
        CloseAction = CloseAction.Destroy,
        Loader = new ComponentLoader()
        {
            Url = Url.Action("ArcTicket", "Archive"),
            Mode = LoadMode.Frame,
            DisableCaching = true,
            ShowWarningOnFailure = true,
            LoadMask = { ShowMask = true, Msg = String.Format("Generating {0} pdf ...",cnt) }
        },
        Buttons = {
            new Button(){ Text="Close & Clear", Icon=Ext.Net.Icon.TableRowDelete, Handler="App.GridPanel1.selModel.deselectAll();App.Windows1.close();"},
            new Button(){ Text="Close", Icon=Ext.Net.Icon.Cancel, Handler="App.Windows1.close();"}
                }
    };
    win.Loader.Params.Add(new Parameter() { Name = "download", Value = download.ToString(), Mode = ParameterMode.Value });
    win.Render(RenderMode.Auto);
}

public FileStreamResult ArcTicket(int download)
{
    String id = TempData["xid"].ToString();
    TempData.Remove("xid");
    String ticket = Uow.TICKETs.GetXmlBatch(id);

    Byte[] pdf = MvcApplication.Fop.Write(enumFobFormat.Pdf
                        , ticket
                        , Server.MapPath("/Resources/xslt/Ticket.xsl")
                        );

    MemoryStream ms = new MemoryStream(pdf);
    FileStreamResult fs = new FileStreamResult(ms, "application/pdf");
    if (download == 1)
        fs.FileDownloadName = string.Format("Archive_{0:yyyyMMdd_HHmmss}.pdf", DateTime.Now);

    return fs;
}

0 个答案:

没有答案