0

What do I need to do to open an excel file via controller action using javascript? I tried the this way but it does not open the file.

JavaScript Function:

function ExcelExport() {

    var link = '/Report/ExcelExportData';

    $.ajax({
        type: 'POST',
        url: link,

        success: function (result) {

        },
        error: function (result) {

        }
    });

};

Controller Action:

public ActionResult ExcelExportData()
        {
            return File("~/Reports/ExcelFile.xlsm", Server.UrlEncode("~/Reports/ExcelFile.xlsm"));

        }
2
  • Is a controller necessary for this? Maybe I'm missing something but could you link it with an anchor tag instead? Commented Oct 4, 2013 at 20:34
  • @Ant it is a static filepath, file is stored in my project folder Commented Oct 4, 2013 at 20:36

1 Answer 1

1

Actually, you can't open files directly from ajax callback responses. You could try two different approaches:

  1. Use an anchor for opening the file and get rid of the ajax request. Something like <a href="/Report/ExcelExportData"> will do.

  2. Call window.open(link) inside your success callback. The only advantage of this approach is that it allows you to handle unexpected errors (file not found or something like that). It will perform another roundtrip to the server, though.

Sign up to request clarification or add additional context in comments.

Comments

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.