简体   繁体   中英

Storing a file on the server AND in the database?

I'm using MVC 3 and SQL Server 2008 R2.

I'm building a File Management tool for my client, so they can store images and pdfs, and then insert them onto pages.

I've been looking into FILESTREAM in SQL Server, and it looks great. It allows me to store the files, as well as keep a backup of them in case something goes wrong.

But I also want the files stored physically somewhere, so that my client can send a link to someone, like: http://www.mysite.com/files/mydoc.pdf

What's the best practice here? Is it safe and/or smart to use both?

If serving up the files via URL is your only reason for storing on the server too, I'd say that you don't want to do this.

In MVC it's trivial to create a controller action that handles that URL, looks up the file in the DB and returns it to the user. The filename is simply an action parameter in this case, your action logic takes that parameter retrieves the file from the database and return it with a FileStreamResult

return File(fileStream, contentType, fileName);

Here's some more info. About half way down the page shows an example of using FileStreamResult

http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM