[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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
Medium Priority
?
437 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
[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
2 Comments
 
LVL 83

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 total points
ID: 39195611
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
ID: 39203235
Thank you very much.  Sorry for the delay in accepting.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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