Showing posts with label sample. Show all posts
Showing posts with label sample. Show all posts

Monday, March 26, 2012

Text file upload

Does anyone know of any sample code out there that allows a simple text file to be uplaoded and then stored in a SQL2005 database?

I have a system to record reports and it would be great to be able to upload and store the reports in the database and then be able to display them again via a web page linked from the record concerned.

regards

for saving a file in the database ... you have to convert it in the Binary array and save it as Binary.

On other hand ... better idea will be ... which i would do in your case ... save just the Text of the file in your Database ... make extra columns for the file meta data contents ... like ... name ... extension .. when it was created ... eidted.

then you will have more room in your database ... and ... processing in both directions will be alot faster...


Why can't you just save the filename in the database and store the actual file in another directory? That way, if you want to display it on a webpage, you can provide a link to the actual file. It would save a lot more time to do it this way then to go through the trouble of saving the document in a database table.

(In my html)
Upload Attachment:

<asp:FileUpload ID="FileUpload1" runat="server" Height="20" Width="300" BorderColor="#BFBFBE"> File Types: (.doc, .pdf, .rtf, .txt, .xls).pdf, .rtf, .txt, .xls)


(In my code behind)
if (FileUpload1.HasFile) {string fileext = Path.GetExtension(FileUpload1.FileName).ToLower();//rtf, doc, pdf, txt, xlsif (fileext ==".doc" || fileext ==".rtf" || fileext ==".pdf" || fileext ==".txt" || fileext ==".xls") {string uniqueFileName = System.Guid.NewGuid().ToString(); HttpPostedFile hp = FileUpload1.PostedFile; hp.SaveAs(@.Request.ServerVariables["APPL_PHYSICAL_PATH"] +"uploads\\" + uniqueFileName.ToString() + fileext.ToString()); } }
Then call your method that will save the uploaded document to the database. This way, you know all documents will be in the uploads folder (and all files will have unique names). When you go to display this in an html page later on, you'll know the location and will just have to pull the filename from the database.

Why can't you just save the filename in the database and store the actual file in another directory? That way, if you want to display it on a webpage, you can provide a link to the actual file

Acutally i would also prefer this one.

Based on my understanding, I think there may be a performance issue if you store the uploaded file in a database. So, storing the meta-data of the file into a db and the file to a file server is a better solution. Also, if the file is too large in size, i would suggest using a activeX upload control.

thanks.


omerkamal:

On other hand ... better idea will be ... which i would do in your case ... save just the Text of the file in your Database ... make extra columns for the file meta data contents ... like ... name ... extension .. when it was created ... eidted.

then you will have more room in your database ... and ... processing in both directions will be alot faster...

i totally agree with omerkamal ; saving just Text of the file in database (if only textfile is tobe uploaded) would be a better idea for fast processing.,

Text files, reading,writing and saving

Hi,

I want a textbox on my site, you write in it and click save

the text is then saved as 'sample.txt'

then I also want that page to OPEN 'sample.txt' and read the contents into a textbox

I just want the simplest way of doing this

can someone point me at the code

thanks

Seethis tutorial which does what you ask.

yes, that answered it

thanks!


AHHH... looks like the right code however...

got a couple of textboxes and buttons

I pasted this code from the tutor into the code area of a button

System.IO.StreamReader StreamReader1=new System.IO.StreamReader(Server.MapPath("test.txt"));TextBox2.Text= StreamReader1.ReadToEnd();StreamReader1.Close();
it says: streamreader is a type in IO and cannot be used as an expression
it also says streamreader is not declared.
im using basic not C is that the problem?

Yes, the tutorial is in C#, not VB. For a VB tutorial trythis page.

yes the code example is written in C

however im using basic

visual web developer express

anyone know how to read write and save a file ...simply

it seems as though I can't use streamreader...there is no help on it in web developer express.


thanks for your help.Geeked

To read.. there's a great new addition to the System.IO namespace and that's "ReadAllText"

So you'd say

Text1.Text = System.IO.File.ReadAllText(Server.MapPath("~/sample.txt"))

and that's it!

For writing, the above links that everyone else is fine... 3 lines using the StringWriter class... and never ever forget to ".Dispose" the object otherwise it will lock the file


You've probably worked this out by now , but I wrote a little tutorial the other day that does exactly this, it over here if you're still intrestedwww.codeproject.com