Razor强类型视图下的文件上传
域模型Users.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FileUpload.Models
{
public class Users
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
控制器:FilesUploadController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FileUpload.Models;
using System.IO;
namespace FileUpload.Areas.FileUploads.Controllers
{
public class FilesUploadController : Controller
{
//
// GET: /FileUploads/FilesUpload/
#region 单个文件上传
public ActionResult Index()
{
return View();
}
#endregion
#region 单个文件上传的方法
[HttpPost]
public ActionResult FileUpload(Users user, HttpPostedFileBase uploadFile)
{
if (uploadFile != null)
{
string username = user.UserName;
string password = user.Password;
if (uploadFile.ContentLength > 0)
{
//获得保存路径
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
}
return View();
}
#endregion
#region 多个文件上传
public ActionResult MultiFiles()
{
return View();
}
#endregion
#region 上传多个文件方法
public ActionResult MultiUpload(Users user, IEnumerable<HttpPostedFileBase> files)
{
string pathForSaving = Server.MapPath("~/Uploads");
string username = user.UserName;
string password = user.Password;
if (this.CreateFolderIfNeeded(pathForSaving))
{
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
var path = Path.Combine(pathForSaving, file.FileName);
file.SaveAs(path);
}
}
}
return RedirectToAction("Index");
}
#endregion
#region 检查是否要创建上传文件夹
private bool CreateFolderIfNeeded(string path)
{
bool result = true;
if (!Directory.Exists(path))
{
try
{
Directory.CreateDirectory(path);
}
catch (Exception)
{
//TODO:处理异常
result = false;
}
}
return result;
}
#endregion
}
}
前端页面:Index.cshtml
@model FileUpload.Models.Users
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("MultiUpload", "FilesUpload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(r=>r.UserName)
@Html.PasswordFor(r=>r.Password)
<input type="file" name="files" id="file1" /><br />
<input type="file" name="files" id="file2" /><br />
<input type="file" name="files" id="file3" /><br />
<input type="submit" value="同时上传多个文件" />
}
Razor强类型视图下的文件上传的更多相关文章
- centos 6.5下安装文件上传下载服务
centos 6.5下安装文件上传下载服务 由于每次在CentOS中要下载一些配置文件到物理机,和上传一些文件到服务器,导致来回的开启ftp软件有点麻烦,这里我们可以使用文件上传下载服务,来解决上传和 ...
- Struts2框架下的文件上传文件类型、名称约定
Struts2框架下的文件上传机制:1.通过multipart/form-data form提交文件到服务器2.文件名是通过什么地方设置的?在strust2的FileUploadInterceptor ...
- linux下将文件上传到svn服务器
linux下将文件上传到svn服务器 摘自:https://blog.csdn.net/sky_yangge/article/details/41544773 2014年11月27日 16:47:57 ...
- ASP.NET MVC下使用文件上传
这里我通过使用uploadify组件来实现异步无刷新多文件上传功能. 1.首先下载组件包uploadify,我这里使用的版本是3.1 2.下载后解压,将组件包拷贝到MVC项目中 3. 根目录下添加新 ...
- 第一零四天上课 PHP TP框架下的文件上传
控制器代码(TestController.class.php) <?php namespace Home\Controller; use Home\Controller\EmptyControl ...
- ssm框架下实现文件上传
1.由于ssm框架是使用Maven进行管理的,文件上传所需要的jar包利用pom.xml进行添加,如下所示: <properties> <commons-fileupload.v ...
- ssm框架下的文件上传和文件下载
最近在做一个ssm的项目,遇到了添加附件和下载的功能,在网上查了很多资料,发现很多都不好用,经过摸索,发现了一套简便的方法,和大家分享一下. 1.在自己已经构建好的maven web项目中 pom. ...
- 微信小程序环境下将文件上传到 OSS
步骤 1: 配置 Bucket 跨域 客户端进行表单直传到 OSS 时,会从浏览器向 OSS 发送带有 Origin 的请求消息.OSS 对带有 Origin 头的请求消息会进行跨域规则(CORS)的 ...
- 将windows下的文件上传到Linux服务器上
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/lx_Frolf/article/deta ...
随机推荐
- springmvc结合freemarker,非自定义标签
参考:http://viralpatel.net/blogs/spring-mvc-freemarker-ftl-example/ 上图: 目录层级: 启动后的访问地址:http://localhos ...
- 转载:js 创建对象、属性、方法
1,自定义对象. 根据JS的对象扩展机制,用户可以自定义JS对象,这与Java语言有类似的地方. 与自定义对象相对应的是JS标准对象,例如Date.Array.Math等等. 2,原型(prototy ...
- poj 1949 Chores 最长路
题目链接 求出最长路..... #include <iostream> #include <vector> #include <cstdio> #include & ...
- FZU Problem 1686 神龙的难题 重复覆盖
题目链接 给出大矩形的长宽, 矩形里面有1,0两个值, 给出小矩形的长宽, 求用最少的小矩形覆盖所有的1. 重复覆盖的模板题. #include <iostream> #include & ...
- ObjectiveC 文件操作一
1,引用和使用文件 NSFileManager 是一个单例对象,在mac应用中可以获取任何地址,在IOS中获取的是相对应的应用程序的地址.可以使用 defaultManager 来得到当前应用程序地址 ...
- HTML编码的用户输入
public string Browse(string genre) { returen HttpUtility.HtmlEncode(genre); } HttpUtility.HtmlEncode ...
- C#对象赋值出现的诡异问题,或许你也遇到过,有待你的解决
前言:今天在代码中,又出现了这个问题,就是对象赋值给一个新的对象时,然后更改新对象中的属性,就会把老对象的值也更改,以前也遇到这个问题,只是没有深究,今天刚好又遇到了此问题,我决定写下来,和大家一起分 ...
- RTTI-CLASS
package com.xt.test; interface Test1Interface { } interface Test2Interface { } class Test1 implement ...
- java核心技术学习笔记之一程序设计概述
Java 核心技术之一程序设计概述 一. Java语言的特点 简单行 :取经于C++,排除了C++不常用的指针.结构等,增加垃圾回收. 面向对象:与C++不同是单继承,但是可以继承多接口.完全面向 ...
- 三种尺寸:手机SIM卡使用指南
毫无疑问目前卖的最火的手机非iPhone 5s莫属,相信仍有不少网友目前处于观望之中,由于iPhone 5s和iPhone 5c采用与iPhone相同的Nano-SIM卡,因此不少新用户在使用之前也徒 ...