MVC 3.0 上传多张图片到服务器
View关键代码:
@using (Html.BeginForm("Create", "Activity", FormMethod.Post, new { enctype = "multipart/form-data" }))
<div class="controls ">
<span class="signup">图片1: <input type="file" name="Image1" />
@Html.TextBoxFor(model => model.Image1, new { @disabled = "disabled", @style = "width:300px;" })
@Html.ValidationMessageFor(model => model.Image1)</span>
</div>
<div class="controls ">
<span class="signup">图片2:
<input type="file" name="Image2" />
@Html.TextBoxFor(model => model.Image2, new { @disabled = "disabled", @style = "width:300px;" })
@Html.ValidationMessageFor(model => model.Image2)
</span>
</div>
<div class="controls ">
<span class="signup">图片3:
<input type="file" name="Image3" />
@Html.TextBoxFor(model => model.Image3, new { @disabled = "disabled", @style = "width:300px;" })
@Html.ValidationMessageFor(model => model.Image3)
</span>
</div>
<p>
<input type="submit" value="保存草稿" name="action" />
<input type="submit" value="发布" name="action" />
</p>
Controller主要代码:
[ValidateInput(false)]
public ActionResult Create(string action, ActivityWeb.Models.activities model, string[] City)
{
foreach (string inputName in Request.Files.AllKeys)
{
HttpPostedFileBase file = Request.Files[inputName];
//存入文件
if (file.ContentLength > )
{
//PropertyInfo pro = model.GetType().GetProperty(inputName);
//string proValue = (string)pro.GetValue(model, null);
//if (!string.IsNullOrWhiteSpace(proValue))
//{
// Common.FileUpload.DeleteFile(proValue); //}
string fileName = Common.FileUpload.UploadFile(file);
// pro.SetValue(model, fileName, null);
}
}
public static class FileUpload
{ //public static char DirSeparator = Path.DirectorySeparatorChar; //public static string FilesPath = string.Format("ActivityFile{0}Images{0}{1}{0}{2}{3}{0}", DirSeparator, DateTime.Now.Year, DateTime.Now.Month.ToString().PadLeft(2, '0'), DateTime.Now.Day.ToString().PadLeft(2, '0'));
//public static string PhysicalPath = HttpContext.Current.Server.MapPath(FilesPath); public static string UploadFile(HttpPostedFileBase file)
{ // Check if we have a file if (null == file) return ""; // Make sure the file has content if (!(file.ContentLength > )) return ""; string fileName = file.FileName; string fileExt = Path.GetExtension(file.FileName); // Make sure we were able to determine a proper // extension if (null == fileExt) return "";
char DirSeparator = '/';
string FilesPath = string.Format("{0}team{0}{1}{0}{2}{3}{0}", DirSeparator, DateTime.Now.Year, DateTime.Now.Month.ToString().PadLeft(, ''), DateTime.Now.Day.ToString().PadLeft(, ''));
string PhysicalPath = HttpContext.Current.Server.MapPath(FilesPath);
// Check if the directory we are saving to exists if (!Directory.Exists(PhysicalPath))
{ // If it doesn't exist, create the directory Directory.CreateDirectory(PhysicalPath); } // Set our full path for saving string path = PhysicalPath + fileName; // Save our file file.SaveAs(Path.GetFullPath(path)); // Return the filename return FilesPath + fileName; } public static void DeleteFile(string fileName)
{
if (!string.IsNullOrWhiteSpace(fileName))
{
// Don't do anything if there is no name if (fileName.Length == ) return; // Set our full path for deleting string path = HttpContext.Current.Server.MapPath(fileName); // Check if our file exists if (File.Exists(Path.GetFullPath(path)))
{ // Delete our file File.Delete(Path.GetFullPath(path)); }
}
} }
MVC 3.0 上传多张图片到服务器的更多相关文章
- .net MVC 简单图片上传
主要完成的是在网页上 上传一张图片到服务器 我搜出来的上传文件代码都特别复杂,对于初学者来说,先解决能上传的问题才最重要,并不需要特别多的功能,仅适合不会上传的初学者,大神请绕路,错误请指出,谢谢 v ...
- 关于最新版AFNetworking(3.0)上传多张图片的问题
最新版的AF已经废弃了很多以前的类,所以很多以前的方法都不能用了,当然最主要还是为了适应ipV6所做的更改.楼主最近正在写多张图片上传,碰到了一些问题,解决之后直接封装了一个方法,废话有点多了,上代码 ...
- Html5+asp.net mvc 图片压缩上传
在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法 ...
- iOS -- 上传多张图片 后台(PHP)代码和上传一张的一样
// 上传多张图片 - (void)send { // 设置初始记录量为0 self.count = 0; self.upcount = 0; // 设置初始值为NO self.isUploadPic ...
- Spring MVC实现文件上传
基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...
- php用jquery-ajax上传多张图片限制图片大小
php用jquery-ajax上传多张图片限制图片大小 /** * 上传图片,默认大小限制为3M * @param String $fileInputName * @param number $siz ...
- 微信JSSDK上传多张图片
之前是使用for循环实现的,但是安卓手机没有问题,苹果手机只能上传最后一张图片. 好在有高手在前面趟路,实用的循环调用.苹果是没有,安卓不清楚.以下内容转自:http://leo108.com/pid ...
- MVC之文件上传1
MVC之文件上传 前言 这一节我们来讲讲在MVC中如何进行文件的上传,我们逐步深入,一起来看看. Upload File(一) 我们在默认创建的项目中的Home控制器下添加如下: public Act ...
- 整理几个js上传多张图片的效果
一.普通的上传图片,张数不限制 <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"&g ...
随机推荐
- mysql之常用操作
1. 数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数 ...
- Bootstrap_响应式网格系统
首先添加CSS样式: [class *= col-]{ background-color: #eee; border: 1px solid #ccc; } [class *= col-] [class ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序更新相关数据
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第八篇:为ASP.NET MVC应用程序 ...
- 教你解决Sublime Text中文乱码问题
教你解决Sublime Text中文乱码问题[转载自成长的企鹅] Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦. ...
- ie7 父元素宽度自适应且为浮动的话 子元素的宽度将不能按比例设置问题
好久没切图,昨天遇到个浏览器兼容的老问题,在ie7下,父元素设置浮动后,其宽度是自适应的,子元素的宽度若没有确定则将显示最小宽度,即文本所占的宽度. 正常其他浏览器显示如下: ie7中显示效果如下: ...
- 用Redis Desktop Manager连接Redis
Redis Desktop Manager是Redis图形化管理工具,方便管理人员更方便直观地管理Redis数据. 然而在使用Redis Desktop Manager之前,有几个要素需要注意: 一. ...
- [css3]CSS3选择器:nth-child和:nth-of-type之间的差异
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1709 一.深呼吸,直 ...
- CEF使用的几个注意点
CEF为chrome浏览器的切入其他浏览器中的轻量级框架. 开发的客户端的时候,这是作为界面显示的首先,可以增强客户的易变性,可塑性. 在开发的过程中(侧重于C,C++解决),遇到的几个问题,以及自己 ...
- 登陆界面Login
最终界面: XMAL 代码: <Grid > <Grid.RowDefinitions> <RowDefinition /> <RowDefinition ...
- C:上台阶
总时间限制: 1000ms 内存限制: 65536kB描述楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可以一步上2阶,也可以一步上3阶,编程计算共有多少种不同的走法. ...