original link
Code Download: Http://files.cnblogs.com/n-pei/mvcfileupload.zip
Recently because teamvideo need to use video and picture upload function, so try to Google a lot of information, and share with you.
1. The most basic file upload.
The view interface posts data to the control (Controller) via form, Controller stores the file to the server, and redirect to other pages.
The code for the View section:
Controller part of the need to determine whether there are post files, files are saved, and then redirected to the index page.
The above example is the simplest, and next we consider adding a jquery UI to it, so that the file is uploaded as follows:
To achieve this, you first need to download several plug-ins for the jquery UI.
To add these scripts to the template:
To upload a simple file uploaded to a div, we need to implement the function is when the mouse clicks on a button, the div will bounce out.
Finally, the div popup script:
Next we consider uploading the picture to the server and then having it displayed on the current page. The results shown are as follows:
In other words, we upload the image to the server side, we need to get this picture creation time, size name and so on.
Filerepository put the file information in a list:
Pass the list to view through the Controller section.
Please refer to the attachment for the code in the View section.
For the image storage, I want to store it in the database next.
First, create the table structure:
Use the Ado.net data model to store the information, so we need to create a edmx file.
Save to picture in the controller section of the database:
View Section Code:
The process is shown below:
Here you need an auxiliary class to help you enter the post for the text. You can see it in the downloaded code.
Uploading pictures and saving them to the database, we also need to show the pictures, which is relatively difficult.
Search for a long time to find the following methods:
Query results after saving pictures to the database:
At the moment I don't know how to show the picture of a list. Hope who can provide a good solution.
The traditional way to store files (Pictures) is already a lot of the above introduction. How to use Ajax methods to store files to the server.
I searched the next, more is the use of jquery form this plug-in to achieve.
You need to download the jquery form plugin and put it in your script folder. and add it to the master template.
When you click on the button, create a form and post it to the server side with Ajax mode. Save mode is not said, the following is the use of jquery form JS code:
In addition to using the jquery form plugin, there are other Ajax ways to implement file uploads:
I give two examples:
1. JQuery & Flash Ajax upload files.
Code Address: http://swfupload.googlecode.com/
Mainly through the flash and JS Interactive Flash program to post data to the background of the controller.
In addition to the Flash section, the other is similar to the code described above. I'll put this part of the code in the Code bag for you to download.
Effect:
2. Pure Jqueryajax Upload file (http://valums.com/ajax-upload/)
This recommendation everybody to look.
Look at this article colleagues, Happy Weekend, hehe.
Resources:
Http://weblogs.asp.net/imranbaloch/archive/2010/04/03/image-preview-in-asp-net-mvc.aspx,
Http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx
Http://www.highoncoding.com/Articles/689_Uploading_and_Displaying_Files_Using_ASP_NET_MVC_Framework.aspx
Http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files
http://valums.com/ajax-upload/
Cheers