[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ASP.NET Downloading a File

Posted on 2014-04-18
5
Medium Priority
?
407 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

649 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