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 开发者_StackOverflow社区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
精彩评论