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. JS闭包导致循环给按钮添加事件时总是执行最后一个

    加入如下脚本代码: <script> var list_obj = document.getElementsByTagName('li'); for (var i = 0; i <= ...

  2. LPCTSTR 用法

    L表示long指针       这是为了兼容Windows  3.1等16位操作系统遗留下来的,在win32中以及其他的32为操作系统中,  long指针和near指针及far修饰符都是为了兼容的作用 ...

  3. 用excel绘制基因芯片热力图

    1.  首先我们通过一些方法得到了如下的数据,基于篇幅以及为了教学隐去了其他一些信息. 2. 选中表达数据,执行 开始—条件格式—色阶 选择一个合适的色阶: 3. 选择好颜色之后得到了如下结果:

  4. Java 中的 static 使用之静态初始化块

    Java 中可以通过初始化块进行数据赋值.如: 在类的声明中,可以包含多个初始化块,当创建类的实例时,就会依次执行这些代码块.如果使用 static 修饰初始化块,就称为静态初始化块. 需要特别注意: ...

  5. Entity Framework 教程

    http://www.cnblogs.com/lsxqw2004/category/266012.html

  6. 焦点问题onfocus=”this.blur()”代替方法(转)

    为了去除链接的虚线框,网上搜索到最常见的方法是onfocus=“this.blur()”,不过同时搜索到的是这会不利于盲人浏览使用页面 在淘宝ued官方博客上详细说明了解决方法,这里转了部分,完整版: ...

  7. git的一些常用方法

    1.撤销add但未commit的文件: git rm -r --cached path/file 2.git 撤销commit: 2.1).git log-显示提交的历史 commit ee50348 ...

  8. HTML基础(1)

    1.前端开发语言介绍 HTML(Hypertext Markup Language)—— 结构—— 超文本标记语言 CSS(Cascading Style Sheets)—— 样式—— 层叠样式表 J ...

  9. git ignore 添加忽略文件不生效解决办法

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: /targ ...

  10. Oracle系统表整理+常用SQL语句收集

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...