Solved

ASP.NET Downloading a File

Posted on 2014-04-18
5
398 Views
Last Modified: 2014-04-26
The following code downloads a file in Firefox or IE and then launches the associated program to open the document. In Chrome it just downloads all the binary data into a document named [webpage].aspx rather than opening it with the correct program (Microsoft Excel for example). It does open PDF documents just fine as long as the Chrome PDF Viewer plugin is enabled. If it's disabled it just downloads a .aspx file again just like the others. Is there any way to tell Chrome to use an application to open the file rather than just download it as a misnamed document?

            GridItem selectedItem = GridMerchantAppAttachments.SelectedItems[0];
            Library.AttachmentRecord attachment = AppSession.CreditApplication.Attachments.FindByName(((GridItem)selectedItem)["FileName"].ToString());

            if (attachment.Data == null)
            {
                attachment.GetData();
            }

            Response.AppendHeader("Content-Disposition:", String.Format(@"attachment; filename=""{0}""", attachment.FileName));

            Response.ContentType = attachment.ContentType;

            Response.AppendHeader("Content-Length", attachment.Data.Length.ToString(Thread.CurrentThread.CurrentCulture));

            Response.BinaryWrite(attachment.Data);
            Response.Flush();

            Response.End();
            Response.Close();

Open in new window

0
Comment
Question by:Russ Suter
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40010229
What is the content type set here?

      Response.ContentType = attachment.ContentType;
0
 
LVL 20

Author Comment

by:Russ Suter
ID: 40012827
It can vary. Sometimes it's application/pdf, sometimes it's application/msword, sometimes it's audio/wav, sometimes application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, or others.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40013437
Actually, I don't believe that there is anything that you can do server-side, since that would open a security hole on the client.
0
 
LVL 20

Accepted Solution

by:
Russ Suter earned 0 total points
ID: 40013443
Oddly I have found a way to make this work. The solution involves placing an iframe on the page then setting the iframe source to a .aspx file where the code behind simply grabs some byte data and sets it to download. Adding the document to the header of the page directly doesn't work in Chrome (though it does in IE and Firefox) possibly due to the fact that there is other HTML content on the page but a blank .aspx page served through an iframe works in all browsers.

Thanks though for your assistance.
0
 
LVL 20

Author Closing Comment

by:Russ Suter
ID: 40024357
A solution was discovered (by accident) that seems to have addressed the problem.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question