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. 关于Extjs MVC模式上传文件的简单方式

    Extjs新手研究上传文件的事情估计是件很头痛的问题,毕竟,我就在头痛.最近两天一直在忙文件上传问题,终于小有收获. 用的是Extjs+MVC3.0+EF开发,语言为C#.前台window代码显示列内 ...

  2. CentOS7网卡的命名规则

    一.前两个字符的含义 en 以太网 Ethernet wl 无线局域网 WLAN ww 无线广域网 WWAN 二.第三个字符的含义 o on-board device index number s h ...

  3. Java开发中经典的小实例-(100能被3整除的数打印出来)

    public class Test21 {    public static void main(String[] args) {        // TODO Auto-generated meth ...

  4. C语言位操作(转)

    http://www.cnblogs.com/cpoint/category/524132.html  

  5. android sdk 更新那些文件

    上篇经验,完成了android开发环境的搭建,相信大家也下载了那1.52G,已经下载好了的Adt_bundle. 那么,我们来点击SDK Manager.exe,看看有些什么吧 2 如图所示,为整个目 ...

  6. 再谈 Unlix (Linux, AIX, HPUX) 上 Java 的 java.lang.OutOfMemoryError: unable to create new native thread

    首先很容易排除是 程序问题 内存用了很少,64 位 Java也没有内存限制,线程也不多,-Xss 堆栈也没人会配置很大. 那么肯定是 limit 不足引起 配置 ulimit 就可以了,问题看起来很简 ...

  7. Creating Materials at runtime And Issue of Shader.Find()

    Creating Materials at runtimehttp://forum.unity3d.com/threads/create-materials-at-runtime.72952/ //通 ...

  8. zoj 1203 Swordfish prim算法

    #include "stdio.h". #include <iostream> #include<math.h> using namespace std; ...

  9. 编译osg的一个错误

    C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets! 提示找不到CMAKELIST.T ...

  10. 【源码】c#编写的安卓客户端与Windows服务器程序进行网络通信

    NetworkComms网络通信框架序言 用c#开发安卓程序 (xamarin.android)系列之三 源码(包含客户端与服务器端所有工程文件)    数据库文件 为了方便您测试,我临时搭建了一个服 ...