locked
File downloading with Ajax problem RRS feed

  • Question

  • User-2059141420 posted

    Hi guys,

    I'm new in mvc. I have faced problem with downloading

    My Ajax code is

    $.ajax({
        type: "POST",
        url: base_url + '/Home/IndexSpecification',
        data: { option_id : 2 },
        //dataType: 'json', encode: true,
        async: false,
        cache: false,
        success: function (data, status, jqXHR) {     
            console.log(data);   
        },
        error: function (jqXHR, textStatus, errorThrown) {
            if (typeof (console) != 'undefined') {
                alert("oooppss");
            } else { 
                alert("error");
            }
        }
    });

    Downloading not working and not even returning any error. Anyone please help. Thanks!

    Saturday, January 12, 2019 3:19 PM

Answers

  • User-552477072 posted

    Hi Robert,

    In your ajax request in success block you have to write this code: 

    $.ajax({
        type: "POST",
        url:'/Home/Download',
        data: { option_id: 2 },
        //dataType: 'json', encode: true,
        async: false,
        cache: false,
        success: function (data, status, jqXHR) {
            window.location = '/Home/Download?option_id=2';
        },
        error: function (jqXHR, textStatus, errorThrown) {
            if (typeof (console) != 'undefined') {
                alert("oooppss");
            } else {
                alert("something went wrong");
            }
        }
    });

    Hopefully, you got that you have to set your option id value globally and declare it in success section.

    Please don't forget to mark as answer if it helps you. Thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 12, 2019 3:33 PM

All replies

  • User475983607 posted

    Hi guys,

    I'm new in mvc. I have faced problem with downloading

    My Ajax code is

    $.ajax({
        type: "POST",
        url: base_url + '/Home/IndexSpecification',
        data: { option_id : 2 },
        //dataType: 'json', encode: true,
        async: false,
        cache: false,
        success: function (data, status, jqXHR) {     
            console.log(data);   
        },
        error: function (jqXHR, textStatus, errorThrown) {
            if (typeof (console) != 'undefined') {
                alert("oooppss");
            } else { 
                alert("error");
            }
        }
    });

    Downloading not working and not even returning any error. Anyone please help. Thanks!

    The technique is to open a new window or iframe with a reference to the file to download.  This is a very common situation and well documented with a Google search.

    https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax

    Saturday, January 12, 2019 3:32 PM
  • User-552477072 posted

    Hi Robert,

    In your ajax request in success block you have to write this code: 

    $.ajax({
        type: "POST",
        url:'/Home/Download',
        data: { option_id: 2 },
        //dataType: 'json', encode: true,
        async: false,
        cache: false,
        success: function (data, status, jqXHR) {
            window.location = '/Home/Download?option_id=2';
        },
        error: function (jqXHR, textStatus, errorThrown) {
            if (typeof (console) != 'undefined') {
                alert("oooppss");
            } else {
                alert("something went wrong");
            }
        }
    });

    Hopefully, you got that you have to set your option id value globally and declare it in success section.

    Please don't forget to mark as answer if it helps you. Thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 12, 2019 3:33 PM