Dave

Dave

  • NA
  • 1
  • 0

Downloading a file stored in a DB and need the user to be able to save or open it. I'm close but need help to finish!

Oct 8 2005 4:29 PM
I've been working on writing an application that lets a user upload a file which is stored in a database. So far I got this to work.

I now want to return the file down to the user so they can save it on their hard drive or open it.  I'm using c# and I've been able get the file out of the database and stored in a byte array.  Now that I have it in a byte array, I want to send it to the browser.

What's happening is ...
- I get the browser window that asks me if I want to save or Open the file.   This is what I want.
- However, the file name shown in the window is the DisplayFile.aspx which is the file name of the page where I have the code that does the downloading. 
- Then, when I choose to Open the file, FrontPage gets opened with ...  System.Byte[] .... as the only text I see.  I was hoping to have Notepad open the file and to see the content of the file, which was not System.Byte[].

Here is the code that I'm having trouble with

// This creates the object used to get the file out of the database
Sql s = new Sql(); 

//Right now I'm using a test file named uploadtest1.txt to test my code. 
// This successfuly gets the file from the DB and into the byte array called file

byte[] file = s.getFile("uploadtest1.txt"); 

// This calls a method that returns the size of the file.  It too is working as intended.
int sz = s.fileSize("uploadtest1.txt"); 

// Here is where my problems start to show up.
// I'll be adding more dynamic features to get the content type, but right now I just want to learn the basics of downloading a file
// Therefore, I set the content type to "text/plain"
// The file I'm sending down was created in Notepad
// First question:  Am I using the correct content type?
Response.ContentType = "text/plain";

// This is sending something to the browser but it does not send the file stored in the byte array named "file"
// Second question:  What am I doing wrong?

Response.OutputStream.Write(file,0,sz);

Thanks in advance for the help. 
czone