Solved

ASP.NET Downloading a File

Posted on 2014-04-18
5
397 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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