开发者

store and display of file from sql database

how to store files (pdf and word files) into sql database and how to display that 开发者_Go百科files with an option of "save" , "open" from sql data base when user click. i am doing project using c# + asp.net web application


You need to do several things here:

1) Create UI page that allows users to upload files.

This page will have a FileUpload control to check for the desired extentions

2) Write code to save these files to a database

The files can be stored as binary blobs. It will be up to you and your application to decide the schema of your database. You may also choose one of many ORM tools to provide you access to the database from your code see

  • Linq2SQL
  • EntityFramework
  • ADO.net
  • Or see Creating A Data Access Layer

You have many choices, choose whatever seems most natural / easy for you.

3) Create a UI for the user to select existing files

This will use your ORM data layer to read back the lists of files and display some sort of buttons / links for the user to select and download

4) Retrieve the files from the database once the user selects one and return the file

Read this MSDN article about returning binary files

Furthermore, google around a bit, you'll probably find lots of existing solutions with frameworks like DNN etc.


To store files, you should check out Filestream from SQL Server 2008: http://msdn.microsoft.com/en-us/library/cc716724.aspx

Other traditional platforms have similar support (with binary or image data types).

An alternative is you can store the file in a shared filesystem, and only store the path to the file in the SQL table.


The most common way is to have two columns in the database for the file to be stored propley. 1 column holds the filename with its extensions(ex: file1.txt) and the 2nd column will be of datatype binary. at the application level. a method gets the uploaded filename and converts it to an array of bytes. then this array is stored in sql in the binary field. the filename is stored in the 1st field. to read the file back, another method will read the binary field from sql and convert it back to a FileStream then save it with the original filename(extension).


Use a fileUploaded to upload the file.

Read the file into a byte array:

byte[] data = System.File.ReadAllByte(File Path);

convert the byte[] to hex and store it in a nvarchhar data field in SQL

stringBuilder sb = new StringBuilder()
foreach(byte b in data)
{
    sb.Append(b.ToString("X");
}

When you need to display it, convert it back to byte[] and create a file out of it, and let the user Open/Save it from there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜