How do I upload photo to sql database using Asp.net MVC
I'm quite new to asp.net MVC. I'm following the NerdDinner tutorial and have created an Edit page to modify the dinner information. This time, I want to somehow have a photo uploaded and save to the database when I press the Save button.
So how can I do this?
If possible, can anyone point me to a 开发者_如何学运维good tutorial that shows me how to upload photos and store it into my sql database?
I'm not familiar with any tutorials, but here are the general steps you would need to take:
- Modify your database and add a byte array column to your table
- Update your ORM model (depending on which version of NerdDinner you are using it might be LINQ to SQL or Entity Framework)
- Modify your edit view to include a
<input type="file"/>
tag - In your POST action method read the file bytes from the request and store them in the database (this post can get you started: http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx)
精彩评论