samedi 23 novembre 2019

the best way to allow locally stored files to be downloaded in asp.net mvc

I have been struggling with a way to allow locally stored files to be downloaded in an asp.net site. I have tried multiple techniques but none seem to work for me. The technique described here does return file data but once I have it back in javascript I cannot get the data to download. I did try putting the file data into a blob with no success. Is there a tutorial someplace I missed?
By the way, I did try <a href=file:///path/to/file/file.pdf>file name</a> with no success. Any tips would be greatly appreciated. All I am looking for is a way to allow a file stored in the local directory to be downloaded by the end user. Here is the javascript I use to retrieve file

            var apiUrl = "/AllSearch/Download";
            var obj = {};
            obj.searchterm = e.target.innerHTML;
            $.ajax({
                type: "GET",
                contentType: "application/json",
                url: apiUrl,
                dataType: "text",
                data: { fileName: obj.searchterm },
                crossDomain: true,
                success: function(response) {
                    var newPDF = new Blob([response], { type: "application/pdf" });
                    var pdfURL = URL.createObjectURL(newPDF);
                    window.open(pdfURL);
                    //var link = document.createElement('a');
                    //link.href = pdfURL;
                    //link.download = "FileName" + new Date() + ".pdf";
                    //link.click();
                },
                error: function (xhr, err) {
                    alert("ready state: " + xhr.readyStat + " " + xhr.status);
                }
            });



Aucun commentaire:

Enregistrer un commentaire