在MVC中保存文件对话框

问题描述 投票:5回答:3

如何在MVC应用程序中创建保存文件对话框?我找不到任何示例。

谢谢。

asp.net-mvc savefiledialog
3个回答
6
投票

[返回文件下载时使用Content-Disposition标头作为附件:

public ActionResult Download()
{
    return File(@"c:\work\report.pdf", "application/pdf", "reoprt.pdf");
}

或者如果要下载的文件是动态生成的:

public ActionResult Download()
{
    byte[] pdf = ... get the contents of the report
    return File(pdf, "application/pdf", "reoprt.pdf");
}

0
投票

对于保存对话框,您必须添加ddl system.windows.forms


0
投票
        string fullpath = "";
        try
        {
            fullpath = ConfigurationManager.AppSettings["FrontEndUrl"].ToString() + "ReportsPdf/" + CustID + "/" + filePdf; //this your url and folder name
            string selectedPath = "";
            Thread t = new Thread((ThreadStart)(() =>
            {
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "PDF Files (*.pdf)|*.pdf";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.FileName = filePdf;
                saveFileDialog1.RestoreDirectory = true;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    selectedPath = saveFileDialog1.FileName;
                    selectedPath = filePdf;
                    Console.WriteLine("writing to: " + saveFileDialog1.FileName); //prints the file to save
                    var wClient = new WebClient();
                    wClient.DownloadFile(fullpath, saveFileDialog1.FileName);
                }
            }));
            // Run your code from a thread that joins the STA Thread
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();
            // e.g C:\Users\MyName\Desktop\myfile.json
            Console.WriteLine(selectedPath);
            return "Done";
        }
        catch (Exception ex)
        {
            Exception.ErrorLog("BOUI", "ReportsController/DownloadFile", ex);
            return "error";
        }
© www.soinside.com 2019 - 2024. All rights reserved.