views:

649

answers:

3

I have stored a txt file as string in a database coulum. Now on one of my aspx page I have a link...I want to open a txt file when the user clicks on this link. How can i read a file from database. I know how to do this using streamreader for a file stored on the disk.

Thanks

+1  A: 

This is going to take some clarification.

If the file is stored as a string in the database, all you have to do is read it into a string and display it in a page.

public void Page_Load(object sender, EventArgs e)
  var text = (from x in dataContext.MyTextTable where x.Id == someId select x.FileText).FirstOrDefault();

  this.textBox.Text = text;
}


Okay, here's what you need to do. Pseudocode follows:

Load the string from the database
Use the ToCharArray() method on the string to get an array of chars Use the HttpResponse object to Write() the char array to the response stream

Here's some almost-compilable code:

public void Page_Load(object sender, EventArgs e)
{
  var text = Repository.GetTextFile(this.FileTextBox.Text).ToCharArray();

  Response.Clear();
  Response.AddHeader("Content-Disposition", "attachment; filename=" & this.FileTextBox.Text);
  Response.AddHeader("Content-Length", text.Length.ToString());
  Response.ContentType = "application/octet-stream";
  Response.Write(text, 0, text.length);
  Response.End();
}

I believe the mime type should be that rather than text/plain as the browser may attempt to open the file rather than saving it as an attachment.

Will
Nope..I want to open the file as an attachment( with an option of saving, saving etc). Sorry if my question was not clear.Thanks
Mithil Deshmukh
A: 

Why do you want to store a file in a database column? Databases are better for the stuff you really need to query... It's useful for queries like 'give me all the given links from the last 30 days' or 'get all blue cars with fabric indoor decorations'.

Files are simply better handled by the OS filesystem. If you must have a record about the file though, you could put a record and instead put the path to the file into the string column.

Cheery
This is the requirement of the project...I don't have a say for this.Thanks
Mithil Deshmukh
+1  A: 

If you really have to store the file in a database (and I fully agree with Will on this, you shouldn't), you may want to think about implementing a separate download page (eg getfile.aspx) which is responsible for getting the string from the database based on an id, perhaps ( getfile.aspx?fileId=12345 ), setting the appropriate HTTP headers and outputting the content direct to the browser. By setting the content type header, you should be able to force the browser to see the aspx page as a txt file, or any other recognisable format of file.

I'd strongly recommend trying to get the project scope changed not to store file in the DB, though, as this method can get very messy very quickly.

ZombieSheep
Yes, thats what I am doing, but the problem is in the actual code. I have a filestream oFileStream = System.IO.File.Open(sFullFilePath, IO.FileMode.Open, System.IO.FileAccess.Read)but I can't use this because I don't have a filepath since I have the contents in a string "sFileToBeProcessed"
Mithil Deshmukh