ASP. net mvc uploads files using the input tag, mvcinput

Source: Internet
Author: User

ASP. net mvc uploads files using the input tag, mvcinput

Some time to learn about ASP. net mvc. Document uploading is a feature that must be supported during development. Previously, the upload was implemented using the third-party control uploadify. Today, the VS standard label input type = "file" is used.

In the controller, create two ActionResult (), the first is the View, and the second is the Action, that is, processing the upload. The difference between the two is that a return View (), A return Content ("").


The uploaded files will be stored in the Temp directory. In the above code example, there is a black arrow of the dotted line, which is the name of the Input type = "file.



Real-time Demonstration:


 


Aspnet mvc20 uploading multiple files

For (fileCount = 0; fileCount <files. Count; fileCount ++)
{
// Define the object for accessing the client to upload files
System. Web. HttpPostedFile postedFile = files [fileCount];
String FileType = postedFile. ContentType. ToString (); // obtain the file type to be uploaded and verify the file header.

String fileName, fileExtension;
// Get the uploaded file name
FileName = System. IO. Path. GetFileName (postedFile. FileName );
// Get the file extension
FileExtension = System. IO. Path. GetExtension (fileName );

// When the file to be uploaded is not empty, verify that the file name and size are consistent. If not, upload is not allowed.
If (FileType = "text/plain" & fileExtension. toLower () = ". txt ") | (FileType =" application/x-zip-compressed "& fileExtension. toLower () = ". zip ") | (FileType =" application/octet-stream "& fileExtension. toLower () = ". rar ") & postedFile. contentLength/1024 <= 1024)
{// Check whether the file header matches the file name to limit the File Upload type. Note: The File Upload types include TXT, ZIP, and RAR, and the file size can only be 1 MB.

If (fileName! = String. Empty)
{
FileName = RandomFileName () + fileExtension;

// Upload File Information
StrMsg. Append ("type of the uploaded file:" + postedFile. ContentType. ToString () + "<br> ");
StrMsg. Append ("client file address:" + postedFile ...... remaining full text>

How does aspnet mvc30 upload images?

<Input type = "file" id = "filePicture" name = "fileData"/>
The parameter HttpPostedFileBase fileData is received in control and is being processed.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.