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 ...
随机推荐
- Windows主机和Linux虚拟机之间传输文件
如果使用VirtualBox的增强功能, 可以实现两者之间文件相互拖拽. 但某些情况下, 比如增强功能安装遇到难以解决的问题, 或者Linux版本为server版本(例如Ubuntu Server发行 ...
- tag标签添加删除并把值存入到一个input的value内
html: <input type="text" id="tagValue" style="display: none;" /> ...
- 通过netty实现服务端与客户端的长连接通讯,及心跳检测。
基本思路:netty服务端通过一个Map保存所有连接上来的客户端SocketChannel,客户端的Id作为Map的key.每次服务器端如果要向某个客户端发送消息,只需根据ClientId取出对应的S ...
- MySQL学习系列一---命令行连接mysql和执行sql文件
1.命令行连接mysql #mysql -h(主机) -u(用户名) -p (数据库名) mysql -hlocalhost -uroot -p testdb Enter password: **** ...
- ODI学习笔记2--ODI产品架构
ODI学习笔记2--ODI产品架构 ODI产品架构: ODI提供了以下几种管理工具:Designer 用于定义数据转换逻辑,这是最常用的开发工具,大部分的开发任务,包括data store的定义,in ...
- Java—异常处理总结
异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C开始,你也许已经知道如何用if...else...来控制异常了,也许是自发的,然而这种控制异常痛苦,同一个异常或者错误如果多个地方出 ...
- HDU 1012 u Calculate e
题解:直接模拟 #include <cstdio> int main(){ puts("n e");puts("- -----------");pu ...
- When Is Cheryl's Birthday
大早上起来逛微博,看见@西瓜大丸子汤Po的一个逻辑题,遂点开看之... 原文链接:http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.i ...
- sicily-1029 Rabbit
一. 题意(0.04s) 每一对成熟的兔子可以生一对兔子,兔子在m个月之后成熟,假设兔子都不会死,计算d个月后一共有多少只兔子. 二. 要高精度加法(用string) 三. ...
- $timeout, $interval
$timeout, $interval layout: posttitle: Angular@1.4.3 中文 API 服务篇 $timeout & $intervaldesc: '$ti ...