[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

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.

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
kwh3856
Asked:
kwh3856
1 Solution
 
David Johnson, CD, MVPOwnerCommented:
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
 
kwh3856Author Commented:
Thank you very much.  Sorry for the delay in accepting.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now