View关键代码:

@using (Html.BeginForm("Create", "Activity", FormMethod.Post, new { enctype = "multipart/form-data" }))
  <div class="controls ">
<span class="signup">图片1:&nbsp;&nbsp;<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:&nbsp;&nbsp;
<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:&nbsp;&nbsp;
<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 上传多张图片到服务器的更多相关文章

  1. .net MVC 简单图片上传

    主要完成的是在网页上 上传一张图片到服务器 我搜出来的上传文件代码都特别复杂,对于初学者来说,先解决能上传的问题才最重要,并不需要特别多的功能,仅适合不会上传的初学者,大神请绕路,错误请指出,谢谢 v ...

  2. 关于最新版AFNetworking(3.0)上传多张图片的问题

    最新版的AF已经废弃了很多以前的类,所以很多以前的方法都不能用了,当然最主要还是为了适应ipV6所做的更改.楼主最近正在写多张图片上传,碰到了一些问题,解决之后直接封装了一个方法,废话有点多了,上代码 ...

  3. Html5+asp.net mvc 图片压缩上传

    在做图片上传时,大图片如果没有压缩直接上传时间会非常长,因为有的图片太大,传到服务器上再压缩太慢了,而且损耗流量. 思路是将图片抽样显示在canvas上,然后用通过canvas.toDataURL方法 ...

  4. iOS -- 上传多张图片 后台(PHP)代码和上传一张的一样

    // 上传多张图片 - (void)send { // 设置初始记录量为0 self.count = 0; self.upcount = 0; // 设置初始值为NO self.isUploadPic ...

  5. Spring MVC实现文件上传

    基础准备: Spring MVC为文件上传提供了直接支持,这种支持来自于MultipartResolver.Spring使用Jakarta Commons FileUpload技术实现了一个Multi ...

  6. php用jquery-ajax上传多张图片限制图片大小

    php用jquery-ajax上传多张图片限制图片大小 /** * 上传图片,默认大小限制为3M * @param String $fileInputName * @param number $siz ...

  7. 微信JSSDK上传多张图片

    之前是使用for循环实现的,但是安卓手机没有问题,苹果手机只能上传最后一张图片. 好在有高手在前面趟路,实用的循环调用.苹果是没有,安卓不清楚.以下内容转自:http://leo108.com/pid ...

  8. MVC之文件上传1

    MVC之文件上传 前言 这一节我们来讲讲在MVC中如何进行文件的上传,我们逐步深入,一起来看看. Upload File(一) 我们在默认创建的项目中的Home控制器下添加如下: public Act ...

  9. 整理几个js上传多张图片的效果

    一.普通的上传图片,张数不限制 <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"&g ...

随机推荐

  1. Packets(模拟 POJ1017)

    Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47750 Accepted: 16182 Description ...

  2. sql索引实例

    1.创建表并插入数据 在Sql Server2008中创建测试数据库Test,接着创建数据库表并插入数据,sql代码如下: USE Test IF EXISTS (SELECT * FROM INFO ...

  3. Android 坐标系统

    屏幕的左上角是坐标系统原点(0,0),原点向右延伸是X轴正方向,原点向下延伸是Y轴正方向. 一.View的坐标     需要注意view的坐标是相对父容器而言的,包括:getTop().getBott ...

  4. sbrk与brk的使用小例子

    sbrk() 和 brk() - Unix的系统函数   sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位.   void* ...

  5. python成长之路【第三篇】:函数

    1.函数基础 函数是python为了代码最大程度的重用和最小化代码冗余而提供的基本程序结构. 函数是一种设计工具,它能让程序员将复杂的系统分解为可管理的部件. 函数用于将相关功能打包并参数. pyth ...

  6. iOS 二维数组排序小算法

    NSArray *tmp = @[@[@(1), @(2), @(3), @(4), @(5)],                     @[@(6), @(7), @(8), @(9), @(10 ...

  7. CSS小三角制作

    以下是参考资料: 好多种图形的:http://www.jb51.net/css/41448.html -------------------------------------15.11.12---- ...

  8. Action<>和Func<> 委托【代理】

    C#中的Action<>和Func<> 其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的 ...

  9. 数据库 基础篇3(mysql语法)

    4 数据库管理(接上篇) 4.1 查询所有数据库 mysql> show databases; +--------------------+ | Database           | +-- ...

  10. ubuntu1404服务器版中设置root用户

    刚安装完没有设置root用户 sudo passwd root 根据提示输入密码,ok . 关闭服务器: shutdown -h now