Download Database File (MVC App)

Download Database File (MVC App)

GreenmoorGreenmoor Posts: 3Questions: 1Answers: 0
edited April 2019 in Free community support

Hello

I'm new to DataTables + Jquery so apologies in advance...

I have a database that contains a stored file in one of the columns (D:\home\site\wwwroot\App_Data\report4324.pdf). I would like to 'mask' the file location with a 'download' URL and when the 'download' URL is clicked the associated file is downloaded.

The file can be named anything but will always be either a doc, docx or pdf.

This doesn't seem to work since the URL is incorrect:

{
"data": "PO",
"render": function (data, type, full, meta) {
return '<a href="' + data + '">Download</a>';
}

Any advice / help will be highly appreciated - thank you!

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    Hi @Greenmoor ,

    That columns.render is the place to go. Can you explain what you mean by "This doesn't seem to work since the URL is incorrect", and give more details on the data and the URL you're trying to create.

    Cheers,

    Colin

  • GreenmoorGreenmoor Posts: 3Questions: 1Answers: 0
    edited April 2019

    Hi @colin

    Thank you for your message. When I use: return '<a href="' + data + '">Download</a>'; and then click on the download link within DataTables, the URL which should download the document from SQL database is file:///D:/home/site/wwroot/App_Data/uploads/report01.pdf which doesnt look right. The end user will not have access to the server's D ://Drive. I think I might have to create some sort of 'GET' action for the datatable to call to trigger the download?

    Thanks again.

    My Class is:

     public class POController : Controller
        {
            // GET: PO
            public ActionResult Index()
            {
                return View();
            }
            public ActionResult GetList()
            {
                using (MEntities db = new MEntities())
                {
    
                    var poList = db.MPO.ToList<MIPurchaseOrder>();
                    List<string> items = new List<string>();
                    return Json(new { data = poList }, JsonRequestBehavior.AllowGet);
                }
            }
        }
    
    My VIew is:
    
    <div style="border:1px solid pink; padding: 12px; width:1200px">
    <table id="PurchaseOrderTable">
        <thead>
            <tr>
                <th>ID</th>
                <th>Date</th>
                <th>ID2</th>
                <th>Name</th>
                <th>Date</th>
                <th>PO</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>ID</th>
                <th>Date</th>
                <th>ID2</th>
                <th>Name</th>
                <th>Date</th>
                <th>PO</th>
            </tr>
        </tfoot>
    </table>
        </div>
    
    <link href="//cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
    @section scripts{
        <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
        <script>
            $(document).ready(function () {
                $("#POTable").DataTable({
                    "ajax": {
                        "url": "/PO/GetList",
                        "type": "GET",
                        "datatype": "json"
                    },
                    "columns": [
                        { "data": "D" },
                        {"data": "Date"},
                        { "data": "ID2" },
                        { "data": "Name" },
                        { "data": "Date"}
                        {
                            "data": "PO",
                            "render": function (data, type, full, meta) {
                                return '<a href="' + data + '">Download</a>';
                            }
                        }
                    ]
                });
    
            });
    
        </script>
    
  • colincolin Posts: 15,146Questions: 1Answers: 2,586
    Answer ✓

    Hi @Greenmoor ,

    Yep, exactly that. Either you have to expose the files so that a URL on the client would be able to access them, or you'll need a server-side script that responds to a GET.

    Cheers,

    Colin

This discussion has been closed.