Simple examples of ASP.NET.MVC single-file uploads and multi-file uploads

Source: Internet
Author: User

First open vs2012, create an empty Mvc4 project, name Mvcstudy, select Basic template

1) After creating the project, the basic structure is like this

2) set up the corresponding HomeController, view index, FileUpload, success, error page

3) Controller Source code

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using System.Web.Services.Protocols;


Namespace Mvcstudy.controllers
{
public class Homecontroller:controller
{
Public ActionResult Index ()
{
return View ();
}

<summary>
Successful return page
</summary>
<returns></returns>
Public ActionResult Success () {
return View ();
}

<summary>
Failed to return to page
</summary>
<returns></returns>
Public ActionResult error () {
return View ();
}

<summary>
Single File Upload
</summary>
<param name= "Upfile" > Objects for uploading Files </param>
<returns></returns>
Public ActionResult FileUpload (HttpPostedFileBase upfile)
{
Try
{
The file is not empty
if (upfile! = null)
{
Create year and month folders, such as 201605
String filefolder = DateTime.Now.ToString ("Yyyymm");
The root path when stitching is saved, for example: d:work/mvctest/uploads/201605
String pathforsaving = Server.MapPath ("~/uploads/") + Filefolder;
Determine if a folder exists or create a folder
if (this. Createfolderisneeded (pathforsaving))
{
Determine the size of the uploaded file
if (upfile. ContentLength > 0)
{
Regroup into a storage path, root path + file name
string filepath = Path.Combine (pathforsaving, Upfile. FileName);
Upfile. SaveAs (filepath);
}
}
Return to Success prompt page
Return Redirecttoaction ("Success");
}
}
catch (Exception e)
{
Return redirecttoaction ("error");
}
return View ();
}

<summary>
Ways to upload Multiple files
</summary>
<param name= "Files" ></param>
<returns></returns>
Public ActionResult Multiupload (ienumerable{
try {
Determine if multiple files are empty
if (Files! = null) {
The storage path of the combined file
String filefolder = DateTime.Now.ToString ("Yyyymm");
String pathforsaving = Server.MapPath ("~/uploads/") + Filefolder;
Determine if the storage path exists or create a corresponding path
if (this. Createfolderisneeded (pathforsaving))
{
Loop through the file and save
foreach (var file in files)
{
if (file! = null && file. ContentLength > 0)
{
var path = Path.Combine (pathforsaving, file. FileName);
File. SaveAs (path);
}
}
}
Return Redirecttoaction ("Success");
}

}
catch (Exception e) {
Return redirecttoaction ("error");
}
return View ();
}

<summary>
Determine if you need to create a folder
</summary>
<param name= "path" > File path </param>
<returns></returns>
public bool Createfolderisneeded (string path) {
bool result = true;
if (! Directory.Exists (path)) {
try {
Directory.CreateDirectory (path);
}
catch (Exception ex) {
result = false;
}
}
return result;
}
}
}
4) The source code of the page

Single-File Upload page

Multi-File Upload page

Success Page

5) File Upload

A, single file upload

b, multi-file upload

C, after uploading the file under the project path

Simple examples of ASP.NET.MVC single-file uploads and multi-file uploads

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.