繁体   English   中英

如何从存储在数据库中的文件路径下载 pdf 文件

[英]How to download pdf files from file path stored in database

我花了大约 3 天的时间试图弄清楚如何下载我使用 LINQ 方法从数据库中检索的文件。 我能够获取存储在我的数据库中的文件路径,但不知道如何从我的 controller 下载它们。

这是我的 controller:

public ActionResult ExportFile(string[] years, string[] months, string[]radio, string[] acctNum)
{
        Statement theStatementPath = new Statement();
        var thePath = theStatementPath.statementPath;

        List<string> allPaths = new List<string>();

        if (years != null)
        {
            using (var db = new dbentities())
            {
                foreach (var aYear in years)
                {

                    List<string> paths = db.Statement
                                           .Where(x => x.statementYear == aYear)
                                           .Select(y => y.statementPath).ToList();
                    StringBuilder builder = new StringBuilder();

                    foreach (var year in paths)
                    {
                        builder.Append(paths).ToString();
                    }

                    string result = builder.ToString();
                    string contentType = "application/zip";
                    byte[] fileBytes = System.IO.File.ReadAllBytes(result);
                    string file = result;

                    return File(fileBytes, contentType, file);
                }
            }
        }

我完全不知道如何 go 关于这一点,我在网上阅读的所有内容似乎都不是我需要的,并且永远不会适用于我想做的事情。 请任何帮助或指导都会很棒!

如果您需要,这是我的 model:

public partial class Statement
{
    public System.DateTime statementDate { get; set; }
    public string statementYear { get; set; }
    public string statementMonth { get; set; }
    public string statementPath { get; set; }
}

这是我的 HTML:Index.cs

<form id="myForm" method="post" action="ExportFile">
    <div class="form-group">
        <div class="my-container">
            <label class="acct-text" for="AccountNumber"> Step 1 - Enter Account Number :</label>
            <input type="text" class="form-control" name="accNum" id="accountNum" placeholder="Account Number">
            <p id="Status"></p>
        </div>
    </div>

    <div class="form-group">
        <label class="year" for="Year">Step 2 - Select Statement Year(s) :</label>

        <div id="checkboxes" class="grid-container2">
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_1"> @ViewBag.StatementYears[0]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_2"> @ViewBag.StatementYears[1]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_3"> @ViewBag.StatementYears[2]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_4"> @ViewBag.StatementYears[3]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_5"> @ViewBag.StatementYears[4]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_6"> @ViewBag.StatementYears[5]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_7"> @ViewBag.StatementYears[6]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_8"> @ViewBag.StatementYears[7]</label>
            <label><input class="year" type="checkbox" name="year" id="chkBoxYear_9"> @ViewBag.StatementYears[8]</label>
        </div>

        <button id="selection" class="select-all-years">Select All Years</button>

    </div>
    <div class="form-group">
        <p><label class="month-text">Step 3 - Select Statement(s) Month :</label></p>
        <div id="checkboxes" class="grid-container">

            <label><input class="month" id="chkbx_jan" name="month" type="checkbox"> @ViewBag.months[0]</label>
            <label><input class="month" id="chkbx_feb" name="month" type="checkbox"> @ViewBag.months[1]</label>
            <label><input class="month" id="chkbx_mar" name="month" type="checkbox"> @ViewBag.months[2]</label>
            <label><input class="month" id="chkbx_apr" name="month" type="checkbox"> @ViewBag.months[3]</label>
            <label><input class="month" id="chkbx_may" name="month" type="checkbox"> @ViewBag.months[4]</label>
            <label><input class="month" id="chkbx_jun" name="month" type="checkbox"> @ViewBag.months[5]</label>
            <label><input class="month" id="chkbx_jul" name="month" type="checkbox"> @ViewBag.months[6]</label>
            <label><input class="month" id="chkbx_aug" name="month" type="checkbox"> @ViewBag.months[7]</label>
            <label><input class="month" id="chkbx_sept" name="month" type="checkbox"> @ViewBag.months[8]</label>
            <label><input class="month" id="chkbx_oct" name="month" type="checkbox"> @ViewBag.months[9]</label>
            <label><input class="month" id="chkbx_nov" name="month" type="checkbox"> @ViewBag.months[10]</label>
            <label><input class="month" id="chkbx_dec" name="month" type="checkbox"> @ViewBag.months[11]</label>

        </div>
        <button id="selection" class="select-all">Select All Months</button>
    </div>

    <p><label for="Delivery">Step 4 - Select Delivery Method :</label></p>
    <p><label><input type="radio" name="radiodecision" id="download" /> Download Statements</label></p>
    <p><label><input type="radio" name="radiodecision" id="email" /> Email Statements</label></p>
    <input type="text" class='txbx' hidden="hidden" />
    <p class="message" hidden="hidden">* To send to multiple recipients, separate the email addresses using a comma "," </p>
    <p class="message" hidden="hidden">* Statement(s) will be delivered via FMBSECURE</p>

    <input type="button" name="submit_form" runat="server" value="RetrieveStatements" id="main-content-submit">

</form>

这是您的问题的一种可能的解决方案

注意:我没有测试过这段代码,所以它可能需要一些小的调整。

我还在代码中添加了一些注释以进一步解释

public ActionResult ExportFile(string[] years, string[] months, string[] radio, string[] acctNum)
{
    Statement theStatementPath = new Statement();
    var thePath = theStatementPath.statementPath;

    List<string> allPaths = new List<string>();

    if (years != null)
    {
        using (var db = new dbentities())
        {
            // we will store the contents of the zip file in memory, 
            // just be careful if you have too many files you might run out of memory
            using (var memoryStream = new MemoryStream())
            {
                // create a zip archive and pass the memory as a parameter
                // you dont need a path, cause as like I said the zip will be in memory
                using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                {
                    foreach (var aYear in years)
                    {
                        List<string> paths = db.Statement
                            .Where(x => x.statementYear == aYear)
                            .Select(y => y.statementPath)
                            .ToList();

                        // iterate all the paths for a given year
                        foreach (var path in paths)
                        {
                            // check if the file at the path exists
                            if (File.Exists(path))
                            {
                                // load the file into the zip archive
                                zip.CreateEntryFromFile(path, path);
                            }
                        }
                    }

                    // once you iterate all years, return the zip file to browser
                    return File(zip, "application/zip", "pdfs.zip");
                }
            }
        }
    }
}

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM