0

I am using elasticsearch to query in index of PDFs on a asp.net mvc app. When I get the result I would like the user to be able to click the returned filename to download that file. Researching how to enable this functionality has returned plenty of results but none seem to work for me. I would simply like the user to be able to click on the filename and have the file download for them. I am using the below code.

To generate the dynamic table with search results

var row = $("<tr><th>" + "Search Results" + "</th><th>" + "File Name" + "</th>");
$("#myTable").append(row);
for (var i = 0; i < response.datasend.length; i++) {
var pos = response.datasend[i].Attachment.Content.indexOf(obj.searchterm);
var row = $("<tr><td>" + response.datasend[i].Attachment.Content.slice((pos - 100), (pos + 100)) + "</td><td><a href=# id=fileName>"
+ response.datasend[i].filename + "</a></td></tr>");
$("#myTable").append(row);
}

To detect the requested filename and call the function to start the download process

    var table = document.getElementById("myTable");
    table.addEventListener("click", function(e) {
    if (e.target && e.target.id == "fileName")
    {
        var apiUrl = "/AllSearch/Download";
        var obj = {};
        obj.searchterm = e.target.innerHTML;
        var params = e.target.innerHTML;
        $.ajax({
            contentType: 'application/json',
            url: apiUrl,
            dataType: "json",
            data: {fileName: obj.searchterm},
            success: function(data) {
                alert("success");
            },
            error: function (xhr, err) {
                alert("ready state: " + xhr.readyStat + " " + xhr.status);
            }
        });
    }
});

To start download the file

public ActionResult Download(string fileName)
        {
            string filename = fileName;
            string filePath = @"C:;at\to\file\Documents\" + fileName;
            byte[] filedata = System.IO.File.ReadAllBytes(filePath);
            string contentType = MimeMapping.GetMimeMapping(filePath);

            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = filename,
                Inline = true,
            };

            Response.AppendHeader("Content-Disposition", cd.ToString());

            return File(filedata, contentType);
        }

The Download function is from Returning a file to View/Download in ASP.NET MVC but it returns an error when I run it. Not sure what I am missing here. Any help is appreciated

EFiore
  • 105
  • 1
  • 9
  • Could you please clarify what you expect from JavaScript code in the post? As well as exact errors you see - "returns an error" is not something folks on SO can work with... – Alexei Levenkov Nov 23 '19 at 01:31
  • Yes of course. I admit I am new to web projects and even newer to asp.net. In any case, I am getting an undefined 200 error. When the user clicks the link I simply want the file to download. I would like to have the JavaScript be receive the File data and open it in a new tab or in the same tab. RIght now it should simply print out a message saying success Please see screenshot [Imgur](https://i.imgur.com/kkZ3Wp4.jpg) – EFiore Nov 23 '19 at 01:54
  • Please [edit] your question instead of comment. Make sure to post error message as text (image is optional). Also make sure to debug your code to see if you can narrow error down (like misspelling properties in the image attached). Side note: approach you took is … unusual - it is generally easier to start with something that everyone else is doing... You may want to visit some web sites and see how they start downloads... if you can't find any check out HTML spec for A tag. – Alexei Levenkov Nov 23 '19 at 03:17
  • As an aside, 200 is success, not an error. – Nikki9696 Nov 23 '19 at 14:43
  • Thanks all. The problem was I left `datatype: json",` in my ajax call. Though I having problems getting the returned file data to download – EFiore Nov 23 '19 at 19:13

0 Answers0