开发者

Storing a file on the server AND in the database?

开发者 https://www.devze.com 2023-03-29 07:48 出处:网络
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'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

0

精彩评论

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

关注公众号