0

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?

2
  • What if you make @Html.ActionLink("Save as...", "GetSurveyFile", "Survey") ? I just don't understand why you need javascript for it. Commented Aug 18, 2015 at 15:39
  • I didn't want to create a link. That's why I wrote "clicking on some text". Commented Aug 18, 2015 at 16:41

1 Answer 1

2

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));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Do I really need to call twice the method?
Actually you don't. You can just write: function getfile() { window.location = '@Url.Action("Download")'; }
I need to use parameters in the Action call. If I just use this the webpage redirects to an empty one, but the Action is never called. var url = '@Url.Action("GetSurveyFile","Survey")?onSurvey=_SurveyId_&onDoc=_filetype_'.replace('_SurveyId_', SurveyId).replace('_filetype_', filetype); window.location = url;
I've added parameters. There is no redirect to empty page. Check out code here (page: ~Views/Home/Index.cshtml, method: HomeController.Download): github.com/kipwoker/StackOverflow-Querstion-32052950

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.