How to open a binary file directly from a database in c#? - database

Hey there,
i have a table with a field called "file" full of binary data (The File Itself) how can i open this binary data directly from the database on a click Event for example using c# ?
i dont want to download the file each time to view it .. just view it using the default viewer in the user's OS.
Any Ideas ??

To start off with other than an extremely trivial (throwaway) application I hope that you have setup some layers to abstract the DB access from your UI.
Ultimately your application will have to get the binary object from the DB table and present it as a file to the OS. The applications that come to mind typically want you to pass a filename to them to open them. In essence this means you have to download the file to the local machine with this technique.
The alternative would be to store the files on the filesystem of a server somewhere and place the name/location in your DB (nvarchar). Your application could get the filename/location and pass that to the viewer application.

Related

Server backend: how to generate file paths for uploaded files?

I am trying to create a site where users can upload images, videos and other types of files.
I did some research and people seem to suggest that saving the files as BLOB in database is a Bad idea; instead, save the file paths in database.
My questions are, if I save the file paths in a database:
1. How do I generate the file names?
I thought about computing the MD5 value of the file name, but what if two files have the same name? Adding the username and time-stamp etc. to file name? Does it even make sense?
2. What is the best directory structure?
If a user uploads images at 12/17/2013, 12/18/2018, can I just put it in user_ABC/images/, then create time-stamped sub-directories 20131217, 20131218 etc. ? What is the best structure for all these stuff?
3. How do all these come together?
It seems like maintaining this system is such a pain, because the file system manipulation scripts are tightly coupled with the database operations(may also need the worry about database transactions? Say in one transaction I updated the database but failed to modify the file system so I need to roll back my database?).
And I think this system doesn't scale (what if my machine runs out of hard disk so I need to upload the files to a second machine? What if my contents are on a cluster?)
I think my real question is:
4. Is there any existing framework/design pattern/db that handles this problem?
What is the standard way of handling this kind of problems?
Thanks in advance for your answers.
I've actually asked this same question when I was designing a social website for food chefs. I decided to store the url of the image in a MySQL database along with recipe. If you plan on storing multiple images for one recipe, in my example, maybe having a comma separated value would work. When the recipe loaded on the page, I would fetch the image associated with that recipe onto the screen.
Since it was a hackathon and wasn't meant for production purposes, I didn't encode the file name into something unique. However, if I were developing for productional purposes, I would append the time-stamp to the media file name when storing it into the server and database/backend.
I believe what I've proposed is the best data structure of handling this scenario. Storing the image onto the server is not only faster, but it should also take less space. I have found that when converting a standard jpg file of reasonable resolution to base64 encoding, the encoded text file representation took 30% more space. There is also the time of encoding the file and decoding the file for storage and resolving when using some BLOB type of data format instead of straight up storing the file on the server.
Using some sort of backend server scripting like PHP, you'll be able to do some pretty neat stuff with the information you have available. Fetch the result from the database, and load it in from the page using HTML.
As far as I know, there isn't a standard way of fetching media from a database yet. Perhaps there will be one day.
There is not standard way to do that, it is different to the different application. The idea is you need generate a different Path+FileName for every upload, here is a way:
HashId = sha1(microsecond + random(1,1000000));
Path = /[user_id]/[HashId{0,2}]/[HashId{-2}];
FileName = HashId

Winforms - Open external pdf file

I've Googled and searched stackoverflow for this but I've not found an answer that is suitable.
All I want to do is place a button a form, the code behind this button will contain a reference to a location on a harddrive where a pdf file is stored. All I want to do is have it so that when the user clicks the button the pdf file opens in the machines selected pdf reader (this is an internal application so the 2-3 machines that access it will always have adobe acrobat on them).
I've no idea which (if any) of the built in .NET classes can do this.
EDIT: There will be 100's of pdfs all added throughout time, I will have a path reference to one pdf per order in my system which points to a file storage server where the PDF lies. The PDF is not locally hosted.
How can this be done in the simplest sense?
You can simply use this code
System.Diagnostics.Process.Start(#"c:\myPdf.pdf");
It will open the file you provide with its associated default program

WinForms and Access Database Attachments

If I have a PDF file saved into an attachment field in an Access database, is there anyway I could get that attachment from the database and view it in the WinForm? Or the WinForm WebBrowser maybe?
Or am I just better off sticking to a field in the database that tells me the file path of said file so I can navigate my WebBrowser to that?
I've been working with Access since long before Access 2007 introduced the Attachments field type, so I have a history of shying away from imbedding images and documents in the database. (They tended to bloat the database quite significantly, and the OLE "wrappers" added to the files were a real nuisance when trying to extract the files via code.)
Access 2007+ makes this quite a bit simpler with the Attachments field because DAO has been updated to support .SaveToFile and .LoadFromFile on Attachments. Also, attachments are (apparently) compressed when saved to the database, which should help with the bloat problem.
So, I'd say that the choice is really up to you, because if you want to view (or preview, or open) the PDF attachment in your WinForm then you'll probably wind up using Microsoft.Office.Interop.Access.Dao to save the attachment as a temporary file anyway. Therefore, whatever mechanism you use to preview/view/open the attachment will be working on a file; it will either be
a temporary file extracted from the database, or
a persistent file in a filesystem that you reference from a pathname or URL in the database.
To read the file from the database you would need to read the bytes from the database and write them to a new pdf file, then point your viewer to that file. To view a pdf directly on the WinForm you would need a 3rd party control. If you have a plugin where you can view pdfs in your browser, the WinForm WebBrowser will work.
Storing just the path in the database leads to less hassle from a coding standpoint, because you are going to have to point your viewer to a file anyway. Also is theres an issue with the database, there is a higher chance all the attachments will be lost. On the flipside, if just the paths are stored you need to make sure those paths are always accessible.
I would recommend storing them outside of the database for the above reasons, especially if this is a larger database.

silverlight: creating a file in memory and offer it for saving it locally

I need to create a file in memory (without saving any copy of it in a filesystem) and then offer it to the user so that he/she can save it in his filesystem. How can I do this in Silverlight?
In Silverlight, access to files can be only user initiated.
This means, saving the file using a SaveFileDialog class.
Follow the link, there is an example there.

creating a video database

I am interested in creating a video databse. My goal is to have a folder where my videos will be kept and each time I copy/delete a video the website that presents them should be updated to. the problem is I have no idea how to approach it.
Should I..
Use Sql and store a reference to each video location?
Have a script that checks all the time if new changes happen in that folder?
A package like joomla?
I am using ubuntu btw. I already have a simple html5 page, and I am presenting the videos using html5 video.
It depends on the size and the performance you want.
1.Way : use php to scan the folder and generate links on the fly
2.way : Use a database to store the file names and retrieve the names from the database and generate urls
pros and cons.
simple to implement , no changes in upload or download script. no database required.
You need have a database , little coding required for upload and also while genrating a page
You should make a db (format does not matter) and storing in it only file names of videos: the videos would be stored on hard drive.
Any operation on the web site will pass first on db for insert/update/delete videos records and then (maybe in a transaction context) on the file system.
This would be the standard approach to your question.

Resources