Solved

ASP.NET Downloading a File

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now