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 ...
随机推荐
- Java中的变量与变量的作用域
关于Java中的变量及变量的作用域 关于Java中的变量及变量的作用域 0. 变量的概念 在程序运行期间,系统可以为程序分配一块内存单元,用来存储各种类型的数据.系统分配的内存单元要使用一个标记符来标 ...
- poj 3501 Escape from Enemy Territory 预处理+二分+bfs
传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...
- 四轴飞行器1.2.3 STM32F407时钟配置和升级标准库文件
原创文章,欢迎转载,转载请注明出处 这个星期进度比较慢哈,只有周末和晚上下班回来才能做,事件不连续,琐碎的事情又比较多,挺烦的,有多琐碎呢? 1.本人有点小强迫症哈,虽然RTT将文 ...
- 面试常用算法——Longest Palindromic Substring(最长回文子串)
第一种: public static void main(String[] args) { String s = "abcbaaaaabcdcba"; int n,m; Strin ...
- Android 流媒体系列(二)
import java.io.IOException; import android.app.Activity; import android.content.ContentResolver; imp ...
- php正则表达式的基本语法
简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例 如,vi编辑器,Perl或PHP脚本语言,以及awk或sed sh ...
- 315M无线发射模块天线的长度计算
波长=光速/频率=300/315=0.952米 1/4波长须要的天线长度=波长*1/4=0.952/4=0.238米 考虑导线传播高频信号的缩短率在0.98左右,因此天线长度=0.238*0.98=0 ...
- C# 动态载入Dll
1.新建測试dll及方法,用vs2010新建winform程序,详细代码例如以下: using System; using System.Collections.Generic; using Syst ...
- list-style:none outside none;的作用
今天在论坛里面看到一篇文章,讲的是以前忽略的一个问题.就是当ul里面有float和display:inline,在ie6.ie7里面会有一些问题.一般对ul进行reset也好,或是设置ul的样式时,往 ...
- No orientation specified, and the default is
链接地址:http://jingyan.baidu.com/article/a24b33cd7722dc19fe002bd0.html No orientation specified, and th ...