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 ...
随机推荐
- Hadoop基础概念介绍
基于YARN的配置信息, 参见: http://www.ibm.com/developerworks/cn/opensource/os-cn-hadoop-yarn/ hadoop入门 - 基础概念 ...
- CPUから広がり
处理技术: 超标量是通过内置多条流水线来同时执行多个处理器,其实质是以空间换取时间.而超流水线是通过细化流水.提高主频,使得在一个机器周期内完成一个甚至多个操作,其实质是以时间换取空间. スター: 真 ...
- sql server 修改表自增列的值
Create PROCEDURE [dbo].[SP_UpdateIdentityId] ( ) , @beforeId INT , @afterId INT ) AS BEGIN IF @befor ...
- 走进Groovy (一)
一直很喜欢脚本语言,但是一直在不大的公司工作,用得一直是“高大上”的JAVA语言,在真正的项目中,没什么机会用到脚本语言.这两年,又断断续续的用了2年的Ruby,再回头继续用JAVA,说实话,真感觉J ...
- 一、ThinkPHP的介绍
一.ThinkPHP的介绍 //了解 MVC M - Model 模型 工作:负责数据的操作 V - View 视图(模板) 工作:负责前台页面显示 编写html代码 C - Controller 控 ...
- dp-史上最戳最长最臭代码-hdu-4733-G(x)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4733 题目大意: 定义G(x)=x⊕(x>>1).给两个由0.1.?组成的长度相同的字符 ...
- MSP430与ATK-NEO-6M GPS模块
近短时间在网上买了一个GPS模块,正好正在学习MSP430单片机,于是决心将GPS模块与MSP430结合起来,同时将代码贴出来,发现网上搜到好多资料都要注册才能下载,有些还要钱.自己动脑,才能自娱自乐 ...
- oracle decode函数用法
DECODE函数是ORACLE PL/SQL是功能强大的函数之中的一个,眼下还仅仅有ORACLE公司的SQL提供了此函数,其它数据库厂商的SQL实现还没有此功能.DECODE有什么用途 呢? 先构造一 ...
- Win7搭建Django开发环境
1.官网下载并安装python 2.7.5 2.配置python 环境变量 在Path中加入python安装目录: PATH=PATH;c:\python26 在PATHEXT中加入以下变量,可以直接 ...
- std::map中函数用法集合
1 STL的map表里有一个erase方法用来从一个map中删除掉指令的节点 2 eg: 3 map<string,string> mapTest; 4 typedef map< ...