javascriptasp.netasp.net-mvc-4asp.net-ajaxfileresult

Save file with prompt 'Save as...' using FileResult


I need to download specific files when clicking on some text. I expect the typical 'Save as..' dialog to choose where I would to save the file, but it doesn't appear. The request and the response are OK.

Request/Response header

GET /Survey/GetSurveyFile?survey=1085&surveyFileType=2 HTTP/1.1

Host: localhost:50518

Connection: keep-alive

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36 OPR/31.0.1889.99

Accept: /

X-Requested-With: XMLHttpRequest

======================

HTTP/1.1 200 OK

Cache-Control: private

Content-Type: application/octet-stream

Server: Microsoft-IIS/8.0

X-AspNetMvc-Version: 5.2

Content-Disposition: attachment; filename="1052__1183__1291__Variable Header Definition.txt"

X-AspNet-Version: 4.0.30319

X-SourceFiles: =?UTF-8?B?UzpcVlNTb3VyY2VcUHJvamVrdGVcTU1JXGJmdWVudGVzXE1NSVxNaW5kc2hhcmUuTU1JXE1NSVxTdXJ2ZXlcR2V0U3VydmV5RmlsZQ==?=

Persistent-Auth: true

X-Powered-By: ASP.NET

Date: Mon, 17 Aug 2015 14:21:48 GMT

Content-Length: 333

My code:

Javascript

function getfile(filetype) {
    var SurveyId = $('#SurveyID').val();
    var url = '/Survey/GetSurveyFile';
    $.ajax({
        type: 'GET',
        url: url,
        data: { survey: SurveyId, surveyFileType: filetype },
        success: function (result) {
            // ?
        },
        error: function (result) {
            // handle errors
            location.href = "/Home/"
        }
    });
}

Controller

public FileResult GetSurveyFile(string survey, string surveyFileType)
{
    try
    {
        var tmpSurvey = EntityModelDataProvider.GetSurveyByID(int.Parse(survey));
        var tmpSurveyFileTypes = EntityModelDataProvider.GetSurveyFileTypes();
        var tmpSurveyFileType = tmpSurveyFileTypes.FirstOrDefault(_sft => _sft.SurveyFile_Type_Id == int.Parse(surveyFileType));
        var tmpFile = EntityModelDataProvider.GetSurveyFilesBySurveyAndType(tmpSurvey.Survey_PK, tmpSurveyFileType.SurveyFile_Type_PK);
        if (tmpFile != null)
        {
            byte[] fileBytes = tmpFile.SurveyFile;
            string fileName = tmpFile.SurveyFile_Name;
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
        }
        else
            throw new Exception("File not found!");
    }
    catch (Exception ex)
    {

        throw ex;
    }
}

Any idea how could I get the desired behavior?


Solution

  • ORIGINAL (read UPDATED part after)

    Look here download file using an ajax request

    I've tried next code on my machine

    function getfile() {
        $.ajax({
            type: 'get',
            url: '@Url.Action("Download")',
            success: function () {
                window.location = '@Url.Action("Download")';
            }
        });
    }
    
    $(function() {
        $('h2').on('click', getfile);
    });
    
    
    public FileResult Download()
    {
        var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/123.txt"));
        return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "123.txt");
    }
    

    UPDATED (v2)

    You needn't ajax request. Change window.location will be enough:

    function getfile() {
        var p1 = Math.random().toString();
        var p2 = Math.floor(Math.random() * 100);
    
        window.location = '@Url.Action("Download")?' + 'p1=' + p1 + '&' + 'p2=' + p2;
    }
    
    $(function() {
        $('h2').on('click', getfile);
    });
    
    
    public FileResult Download(string p1, int p2)
    {
        var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/123.txt"));
        return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, string.Format("123_{0}_{1}.txt", p1, p2));
    }