We have a solution that converts an ActiveReport based report into an Excel file. Basically that works very fine, but in some environments for some very strange reason the downloading of the Excel file doesn't start and the Excel file opens on a web browser window instead of asking if the user wants to open or save the generated Excel file.
Our application is running on a Windows Server 2019 based virtual machine. However, if I connect to the web server on that virtual machine from other computer and open the same application and export the same report into an Excel file everything works fine. The web browser asks if I want to save or open the Excel document and I can open that fine to Excel or Excel viewer.
One more strange thing is, that this problem doesn't appear in every server running this same application. It also seems, that if there is only Microsoft Office Excel viewer installed on the server, these problems appear - not in every case though. If full version of Excel is installed, it seems, that there are no problems.
We have tried everything what we have found. We have set the browser flags ( https://learn.microsoft.com/en-US/troubleshoot/browsers/how-to-set-ie-to-open-office-documents-in-office-program and https://www.thewindowsclub.com/make-internet-explorer-open-linked-office-documents-in-the-appropriate-office-program ) and went through dozens of web pages and reinstalled Excel Viewer.
Still, it seems that this problem is not related to browser flags at all, since this works fine in other servers even the browser flag values are not set in registry keys.
So, to me the problem seems to be in web browser settings on the server. I just cannot imagine where. The web browser installed on the server is IE 11.
Any help is highly appreciated.
EDIT:
As I explained in my comment I discovered, that there are differences in versions of this application where this problem occurred.
The differences between versions are:
public static void ReportExport(DataDynamics.ActiveReports.Document.Document doc,
ReportExportOptions exportOptions, string exppath,
MemoryStream expstream, HttpResponse resp)
{
// For exporting to Response
string contenttype = "";
contenttype = "application/vnd.ms-excel";
// Current version, working.
// Write to Response
if (resp != null)
{
resp.Clear();
resp.Charset = "";
resp.ContentType = contenttype;
byte[] expArray = expstream.ToArray();
if (exportOptions.ExportFormat == ReportExportOptions.ExportType.Xls)
{
resp.AddHeader("content-disposition", "attachment; filename=\"ExcePreview.xls\"");
resp.AppendHeader("content-length", expArray.Length.ToString());
resp.Buffer = true;
}
expstream.Position = 0;
resp.BinaryWrite(expArray);
resp.OutputStream.Flush();
resp.OutputStream.Close();
resp.Flush();
resp.Close();
resp.End();
}
// Previous version, not working
// Write to Response
if (resp != null)
{
resp.Clear();
resp.Charset = "";
resp.ContentType = contenttype;
expstream.Position = 0;
resp.BinaryWrite(expstream.ToArray());
resp.End();
}
}
EDIT 2: I tested this on a server that had both Microsoft Excel 2016 and Microsoft Office Excel Viewer installed. Also this web page that export Excel files is installed on that server. The application that I tested used this kind of content type settings:
// Previous version, not working
// Write to Response
if (resp != null)
{
resp.Clear();
resp.Charset = "";
resp.ContentType = contenttype;
expstream.Position = 0;
resp.BinaryWrite(expstream.ToArray());
resp.End();
}
First, when Excel was installed, the Excel-export was working fine. The Excel 2016 was opened with the exported Excel file when I selected "Open".
Then I uninstalled Excel. The same Excel-export behaved like explained in the beginning of this case: Excel Viewer didn't open and the Excel 'file' was opened on browser window.
The one who can explain this will get the reward.
Regarding your first edit, the important thing is the Content-Disposition
header. The working version sets this header to attachment
. This tells the browser that the file is some sort of download and should be opened with another program or saved. In addition it sets the filename to 'ExcePreview.xls'.
If you do not set Content-Disposition
the default value is inline
telling the browser, that the content is a web page or should be displayed in a web page. The filename stays ExcelPreview.aspx as we see in your first screenshot.
Some systems may consider the Content-Type
header, ignore the aspx extension, sniff the content and treat the response as a downloaded excel file but there is no guarantee this will work.