Solved

Code to download data from webpage.  File does not download.....IE displays the file instead of giving you the option to save the file to the local disk.

Posted on 2013-05-24
2
423 Views
Last Modified: 2013-05-28
I have created a page where the users clicks a button inside a webgrid to download a file.  The row data is selected and the specific record is pulled from the database using LINQ.  The record found contains a binary field with some type of file stored in this field.  I want the page then to ask the user if they would like to save this file to the local hard drive.

What is currently happening is Internet Explorer attempts to display the content of the file.  For example, if the file was a .jpg, it displays the file on the screen.  It is nice but I want the file to prompt the user for downloading to their local machine.  Can someone tell me what I am doing wrong to make this code perform as I need it to.

Any help is greatly appreciated.


if (oButton.Text == "Download")
                {

                     DBDataContext dc= new DBDataContext();


                        string UploadIndex;

                        UploadIndex = oRow.Items[0].Value.ToString();


                        Guid guid = new Guid(UploadIndex);

                        

                        var UpLoadResult = from UpLoadData in dc.UpLoadBinaries
                                           where UpLoadData.UploadBinaryIndex == guid

                                           select UpLoadData;


                        
                        byte[] DownloadData = null;


                        // Pull the record
                        foreach (var UpLoadData in UpLoadResult)
                            try
                            {
                              
                                WebClient req = new WebClient();
                                HttpResponse response = HttpContext.Current.Response;
                                response.Clear();
                                response.ClearContent();
                                response.ClearHeaders();
                                response.Buffer = true;
                             
                                DownloadData = UpLoadData.Filedata.ToArray();
                                
                                response.BinaryWrite(DownloadData);
                                response.End();
                            }
                            catch (Exception ex)
                            {
                            }

                    }
                }

            }

Open in new window

0
Comment
Question by:kwh3856
2 Comments
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
Comment Utility
we need to know what datatype is in the sql blob

               For Example ABC IS DATABASE FIELD OF Image DataType,
                 FileName, ContentType  is also saved in Database
                ContentType Examples are
                         Response.ContentType = "image/jpeg";
                         Response.ContentType = "image/gif";

 Only Need this Code to Download,

                         Response.ContentType = "FromDataBase";
                         string FileName = "FromDbData.jpeg"; //or gif or png as saved in DataBase
                         Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
                         Response.BinaryWrite(ABC );

http://forums.asp.net/t/1396909.aspx/1
0
 

Author Closing Comment

by:kwh3856
Comment Utility
Thank you very much.  Sorry for the delay in accepting.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now