0

I have no response on the browser even though there are no error in my code and I have no idea what went wrong.

Code reach return File(memoryStream, contentType, "test" + extension); without error but I get no response from browser.

[HttpPost]
public ActionResult DownloadAttachment(eNotice.attachment attachment)
{
    try
    {
        var attachmentToDownload = db.Tbl_ENoticeAttachements.Where(a => a.ENID == attachment.ENID && a.IsDeleted == false && string.Compare(a.FileName, attachment.FileName, StringComparison.OrdinalIgnoreCase) == 0).FirstOrDefault();
        if (System.IO.File.Exists(attachmentToDownload.FilePath + @"\" + attachmentToDownload.FileName))
        {
            var file = System.IO.File.ReadAllBytes(attachmentToDownload.FilePath + @"\" + attachmentToDownload.FileName);
            var extension = Path.GetExtension(attachmentToDownload.FileName);
            MemoryStream memoryStream = new MemoryStream(file);
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Seek(0, SeekOrigin.Begin);
            var contentType = System.Web.MimeMapping.GetMimeMapping(attachmentToDownload.FileName);
            return File(memoryStream, contentType, "test" + extension);
        }
        else
        {
            return null;
        }
    }
    catch (Exception exception)
    {
        exception = exception;
        return null;
    }
}

The code above is call via javascript:

$('#buttonTest').click(function () {
    var ENID = $(this).data('enid');
    var FileName = $('#buttonTest').val();
    var formData = "{FileName:'" + FileName + "',ENID:'" + ENID + "'}";
    makeAjaxCall("", '@Url.Action("DownloadAttachment", "eNotice")', formData, "", "", "");
});
Pop
  • 525
  • 1
  • 7
  • 22
  • How are you calling this action? – JamesS Apr 22 '20 at 09:58
  • @JamesS question updated. – Pop Apr 23 '20 at 01:13
  • 2
    The response will be returned to the AJAX call, not to the browser. But you might be able to make the browser download that file through JS code, once you retrieve the file response via JavaScript. Check out this page: https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server – Anthony McGrath Apr 23 '20 at 01:33
  • Also may help you: https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc – Anthony McGrath Apr 23 '20 at 01:40

1 Answers1

0

This is what I do: data is, in my case, is just a file name and a file extension but yours, by the looks of it, is a file.

$.ajax({
            url: '@Url.Action("// Action", "// Controller")',
            type: 'POST',
            data: JSON.stringify(data),
            contentType: 'Application/json',
            success: function (result) {
                if (result.success) {
                    window.location.href = '@Url.Action("// Action", "// Controller")
                }
                else {
                    debugger;
                    errorNotification("Error! " + result.message);
                }
            }
        });

In my controller I have a global static byte[].

The ajax call calls a controller method which gets a file and saves it as a byte array and then I set the global static byte[] as result of this. After this you can return whatever from this function, I've returned a bool and a messages if the conversion to the byte[] failed.

In the success of the ajax I have a windows.location.href which calls a controller action that simply returns the File to the view.

return File(_fileContents, "application/force-download", "test" + extension);

_fileContents is in my case, the name of the global static byte[] set earlier.

JamesS
  • 2,167
  • 1
  • 11
  • 29