我试图根据所选的复选框从数据库中下载一个文件,我正在使用ajax调用从复选框中获取值,并将其发送到我的控制器。我正在使用ajax调用从复选框中获取值,并将其发送到我的控制器。然后我使用SQL查询调用一个函数来抓取那个特定的文件。 下面是我的控制器。
public class HomeController : BaseController
{
public ActionResult Index()
{
// Get all Years from db
ViewBag.StatementYears = _db.ClientStatementsGenerator_GetClientStatementYears().ToList();
// Get all Months from db
ViewBag.months = _db.ClientStatementsGenerator_GetClientStatementMonths().ToList();
return View();
}
[HttpPost]
public ActionResult Extract(string[] response)
{
DBQueries querying = new DBQueries();
querying.GetSpecificYears(response);
return File(querying, "applicaiton/pdf");
}
[HttpPost]
[Route("")]
public FileResult Download(string year)
{
return File();
}
}
}
我的视图和JQuery。
$(document).ready(function () {
// Get the text values of year checkboxes
$('#main-content-submit').click(function () {
var labelArray = [];
labelArray = $("input:checkbox:checked").map(function () {
return $(this).closest('label').text().trim();
}).get();
console.log(labelArray);
event.preventDefault();
var rGrp = $("input[name=filedecision]");
rGrp.map(function () {
var checkedRadio = rGrp.filter(":checked");
console.log(checkedRadio.toArray());
return $(this).text(checkedRadio.val());
}).get();
$.ajax({
type: 'POST',
url: '/Home/Extract',
data: JSON.stringify({ response:labelArray }),
contentType: 'application/json; charset=utf-8',
success: function (result) {
alert("Month and year passed to controller");
},
error: function (err, result) {
alert("Error in Extract" + err.responseText);
},
});
return false;
});
<form id="myForm" method="post" action="Extract">
<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="type" id="chkBoxYear_1"> @ViewBag.StatementYears[0]</label>
<label><input class="year" type="checkbox" name="type" id="chkBoxYear_2"> @ViewBag.StatementYears[1]</label>
<label><input class="year" type="checkbox" name="type" id="chkBoxYear_3"> @ViewBag.StatementYears[2]</label>
<label><input class="year" type="checkbox" name="type" id="chkBoxYear_4"> @ViewBag.StatementYears[3]</label>
<label><input class="year" type="checkbox" name="type" id="chkBoxYear_5"> @ViewBag.StatementYears[4]</label>
<label><input class="year" type="checkbox" name="type" id="chkBoxYear_6"> @ViewBag.StatementYears[5]</label>
<label><input class="year" type="checkbox" name="type" id="chkBoxYear_7"> @ViewBag.StatementYears[6]</label>
<label><input class="year" type="checkbox" name="type" id="chkBoxYear_8"> @ViewBag.StatementYears[7]</label>
<label><input class="year" type="checkbox" name="type" 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="filedecision" id="download" /> Download Statemtents</label></p>
<p><label><input type="radio" name="filedecision" id="email" /> Email Statemtents</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="submit" name="submit_form" value="RetrieveStatements" id="main-content-submit">
</form>
</body>
这是一个我试图使用的函数。任何帮助将是非常感激的。我以前从来没有做过这个,也不知道该用什么库或类来实现这个功能。
public void GetSpecificYears(string[] response)
{
// Acquiring SqlConnection as a resource
using (SqlConnection con = new SqlConnection("****"))
{
//Open Connection
con.Open();
if (response != null)
{
for (var i = 0; i < response.Length; i++)
{
if (response[i] == "2019")
{
// Using SqlCommands based on the SqlConnection
using (SqlCommand cmd = new SqlCommand("select statementPath from ClientStatement_Inventory where statementYear = '" + response[i] + "';", con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader);
}
}
}
}
}
}
}
如果是我的话,我会这样下载文件,在控制器中我将有
[Route("~/FileName")]
[Produces("text/csv")] //In your find the pdf equivalent
public ActionResult ExportFile()
{
StringBuilder sb = new StringBuilder();
var files = DB.ClientStatement_Inventory.Where(a => a.StatementYear == "???")
.ToList();
sb.AppendLine("ColumnName1" + ", " + "ColumnName2" + ", " + "ColumnName3" + ", ");
foreach (var line in files)
{
sb.AppendLine(line.ColumnName1 + ", " + line.ColumnName2 + ", " +
line.ColumnName3 + ", ");
}
sb.ToString();
return File(System.Text.Encoding.ASCII.GetBytes(sb.ToString()), "text/csv", "FileName.csv");
}
然后在视图中只要添加一个链接到这个方法的按钮,就可以下载了,不过你需要找到pdf等价物,并确保你有所有必要的库,以实现 StringBuilder
诸如此类。但试试这个,看看你会得到什么。