zoukankan      html  css  js  c++  java
  • MVC文件上传

    控制器

       1:  using System;
       2:  using System.Collections.Generic;
       3:  using System.IO;
       4:  using System.Linq;
       5:  using System.Web;
       6:  using System.Web.Mvc;
       7:   
       8:  namespace MvcApplication2.Controllers
       9:  {
      10:      public class HomeController : Controller
      11:      {
      12:          public ActionResult Index()
      13:          {
      14:              return View();
      15:          }
      16:   
      17:          public ActionResult FileUploads()
      18:          {
      19:              string pathForSaving = Server.MapPath("~/Uploads");
      20:              if (this.CreateFolderIfNeeded(pathForSaving))
      21:              {
      22:                  foreach (string file in Request.Files)
      23:                  {
      24:                      HttpPostedFileBase uploadFile = Request.Files[file] as HttpPostedFileBase;
      25:                      if (uploadFile != null && uploadFile.ContentLength > 0)
      26:                      {
      27:                          var path = Path.Combine(pathForSaving, uploadFile.FileName);
      28:                          uploadFile.SaveAs(path);
      29:                      }
      30:                  }
      31:              }
      32:              return RedirectToAction("Index");
      33:          }
      34:   
      35:          // 检查是否要创建上传文件夹
      36:          private bool CreateFolderIfNeeded(string path)
      37:          {
      38:              bool result = true;
      39:              if (!Directory.Exists(path))
      40:              {
      41:                  try
      42:                  {
      43:                      Directory.CreateDirectory(path);
      44:                  }
      45:                  catch (Exception)
      46:                  {
      47:                      //TODO:处理异常
      48:                      result = false;
      49:                  }
      50:              }
      51:              return result;
      52:          }
      53:      }
      54:  } 

    □ Home/Index.cshtml视图

       1:  @{
       2:      ViewBag.Title = "Index";
       3:      Layout = "~/Views/Shared/_Layout.cshtml";
       4:  }
       5:   
       6:  @using (Html.BeginForm("FileUploads", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
       7:  {
       8:      <input type="file" name="files1" id="file1" /><br/>
       9:      <input type="file" name="files2" id="file2" /><br/>
      10:      <input type="file" name="files3" id="file3" /><br/>
      11:      <input type="submit" value="同时上传多个文件" />
      12:  }
    注意: 
    name属性值可以不同

    转载自:http://www.csharpwin.com/dotnetspace/13612r1616.shtml

  • 相关阅读:
    Android特效专辑(一)——水波纹过渡特效(首页)
    简单整数算术运算
    Python 获取新浪微博的热门话题 (API)
    比真机还快的Android模拟器——Genymotion
    兔子--CheckBox与Radiobutton的差别
    hdu oj 3127 WHUgirls(2009 Asia Wuhan Regional Contest Online)
    python中列表,元组,字符串如何互相转换
    典型LoadRunner脚本
    LoadRunner显示中文乱码的问题
    如何解决paramiko执行与否的问题
  • 原文地址:https://www.cnblogs.com/lxxhome/p/5663353.html
Copyright © 2011-2022 走看看