ASP.net MVC - I'm lost in downloading file from database

Viewed 288

I am trying to download a file from my database based on the selected checkbox. I am getting the value from the checkbox using an ajax call and having it sent to my controller. I am then calling a function with SQL queries to grab that specific file. Here is my controller:

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();
    }

} 

}

My View and 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>

Here is a Function I am trying to use. Any help would be greatly appreciated. I have never done this before and don't really know what libraries or classes to use to achieve this.

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);
                                }
                            }

                        }
                    }
                }
            }
        }
0 Answers
Related