'How to open a excel file after it gets downloaded in javascript?
I download an excel file (.xls) by using below code:
JavaScript Code:
window.location = result.filename;
After downloading, I want to open a excel file automatically without clicking on it. I want JavaScript code for opening excel file automatically.
I tested with following function code. But it runs only in Internet Explorer, not in Mozilla, Chrome...
function openExcelFile(strFilePath) {
var objExcel;
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = true;
objExcel.Workbooks.Open(strFilePath);
}
By using above code, the excel file open automatically after it gets downloaded in Internet Explorer.
I want JavaScript code for opening excel file automatically after downloaded by working in all browsers.
How can I achieve this?
Solution 1:[1]
pass the excel file path to the controller. it will works fine with all browsers.
public ActionResult GetFile(string path)
{
string extension = new FileInfo(path).Extension;
if (extension != null || extension != string.Empty)
{
switch (extension)
{
case ".xls":
return File(path, "application/vnd.ms-excel");
case ".xlsx":
return File(path, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
return View("Index");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
