Reputation:
I have spent about 3 days on trying to figure out how to download my files I am retrieving from the database using a LINQ Method. I am able to get the file paths that are stored in my database but have no idea how to download them from my controller.
Here is my 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);
}
}
}
I have absolutely no idea how to go about this and everything I read online doesn't seem like what I need and never works for what I am trying to do. Please any help or guidance would be so awesome!
Here's my model if you need it:
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; }
}
Heres is my 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>
Upvotes: 1
Views: 609
Reputation: 803
Here's one possible solution to your problem
Note: I haven't tested this code so it might need some little tweaks.
I've also put some comments in the code to explain it further
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");
}
}
}
}
}
Hope this helps
Upvotes: 1