Wednesday, October 19, 2016

How to download a file through ajax request in asp.net MVC 4

public ActionResult DownloadAttachment(int requestParameter)
{          
    
    //blah blah

    byte[] fileBytes = System.IO.File.ReadAllBytes(file.Filepath);

    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, file.Filename);                       

}

$(function () {
        $("#DownloadAttachment").click(function () {
            $.ajax(
            {
                url: '@Url.Action("DownloadAttachment", "PostDetail")',
                contentType: 'application/json; charset=utf-8',
                datatype: 'json',
                data: {
                    requestParameter: 123
                },
                type: "GET",
                success: function () {
                    window.location = '@Url.Action("DownloadAttachment", "PostDetail", new { requestParameter= 123 })';                }
            });

        });
    });

No comments:

Post a Comment