?
Solved

ASP.NET Downloading a File

Posted on 2014-04-18
5
Medium Priority
?
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

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