这是在Control里使用的代码,是在后台管理需要上传图片时使用的,不过我在这犯了一个错误, Request.Files[inputName];inputName名字中的大小写
<input type="file" name="image">应该与model里的字段的名字一致
if (Request.RequestType == "POST")
            {
                foreach (string inputName in Request.Files.AllKeys)
                {
                    HttpPostedFileBase file = Request.Files[inputName];
                    //存入文件
                    if (file.ContentLength > 0)
                    {
                        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 string UploadFile(HttpPostedFileBase file)
        {

// Check if we have a file

if (null == file) return "";

// Make sure the file has content

if (!(file.ContentLength > 0)) 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(2, '0'), DateTime.Now.Day.ToString().PadLeft(2, '0'));
            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 == 0) 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));

}
            }
        }

mvc3 上传图片的更多相关文章

  1. 关于Asp.Net Mvc3.0 使用KindEditor4.0 上传图片与文件

    http://blog.csdn.net/fyxq14hao/article/details/7245502 今天我们的Asp.Net Mvc 3的项目中,把KindEditor3.9改为 KindE ...

  2. 在MVC3中修改KindEditor实现上传图片到指定文件夹

    KindEditor编辑器默认上传的图片文件夹,是根据系统时间自动生成的,图片是自动上传到这些文件夹里面,无法选择.如果要上传图片到指定文件夹,像相册一样管理图片,则需要扩展KindEditor编辑器 ...

  3. 在MVC3中使用富文本编辑器:KindEditor的配置及上传图片

    现在比较常用的富文本编辑挺多的,如ueditor.fckeditor.kingeditor等,本文主要介绍一下KindEditor的配置与使用. 先去官网http://www.kindsoft.net ...

  4. MVC3.0 上传图片并生成缩略图

    转自:http://mikelai.blog.163.com/blog/static/18411126620118771732675/ Controller: public ActionResult ...

  5. 在MVC3或asp.net中修改KindEditor实现上传图片时添加水印

    主要修改两个文件:image.js和upload_json.ashx文件. 一.修改image.js文件 打开kindeditor/plugins/image目录下的image.js文件,找到 '&l ...

  6. 【读书笔记】Asp.Net MVC 上传图片到数据库(会的绕行)

    之前上传图片的做法都是上传到服务器上的文件夹中,再将url保存到数据库.其实在MVC中将图片上传到数据库很便捷的事情,而且不用去存url了.而且这种方式支持ie6(ie6不支持jquery自动提交fo ...

  7. 网页上传图片 判断类型 检测大小 剪切图片 ASP.NET版本

    本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=56&extra=page%3D1 我们在网页上传图片的时候,特 ...

  8. 百度Web富文本编辑器ueditor在ASP.NET MVC3项目中的使用说明

    ====================================================================== [百度Web富文本编辑器ueditor在ASP.NET M ...

  9. MVC3中 swfupload 按钮不显示 解决方案

    这两天在做图片上传并显示的功能,之前就用过swfupload,觉得很不错,之前是用asp.net webform做的,这次的项目是用asp.net MVC3来做,视图引擎用的是Razor. 将js文件 ...

随机推荐

  1. 让ie6对png透明图片支持起来

    [声明:此文仅是对低版本ie使用透明图片的一个研究,当时出于工作要求,所以花费了一番心思在兼容旧版本ie上,现在对ie8都是做降级处理了.不培养用户坏习惯.引导用户跟随潮流体验新技术应是我们前端开发者 ...

  2. JavaEE MyBatis

    1.  简介 MyBatis本是apache的一个开源项目iBatis的升级版,2013年11月迁移到Github,是三层架构中持久层框架. 目前提供了Java..NET.以及Ruby三种语言实现的版 ...

  3. 键盘对应的ASCII码

    ESC键 VK_ESCAPE (27)回车键: VK_RETURN (13)TAB键: VK_TAB (9)Caps Lock键: VK_CAPITAL (20)Shift键: VK_SHIFT ($ ...

  4. stm8s103 PWM

    stm8s103 PWM的设置不难,但是很多人不注意选项字节这个问题,PWM是IO口的第二功能,要用ST Visual Programmer 修改选项字节. 只需要修改AFR0的功能就可以了

  5. visor 发布

    2014-4-10 visor.com.cn  的域名备案终于审核通过了.http://www.visor.com.cn 终于可以访问了,欢迎大家使用免费的线框图应用设计工具.

  6. 【译】Android 6.0 Changes (机翻加轻微人工校对)

    Android 6.0 Changes In this document Runtime Permissions Doze and App Standby Apache HTTP Client Rem ...

  7. HalconMFC(三)之打开图像_简单处理

    在VS2010中配置完halcon之后就得写个最简单的程序满足一下咱们的自信心呀! 第一步,新建MFC程序 最好是按照下面这么设置然后点击"完成",就不会报错 第二步,导出C++代 ...

  8. Lombok简化Java代码

    导包:import lombok.Data; Lombok简化Java代码: 在Lombok中,生成构造方法的annotation一共有三个:@NoArgsConstructor, @Required ...

  9. No matching provisioning profile found: Your build settings specify a provis...

    解决办法: (1)右键.xcodeproj文件,显示包内容--->找到project.pbxproj文件打开--->查找“PROVISIONING_PROFILE"---> ...

  10. 我是一只IT小小鸟读后感

    当老师推荐我读这本书的时候,并不想看,因为我不喜欢机械的东西,然而阅读几章后,对这本书有了其他看法.不知不觉竟把它看完!看完这本<我是一只IT小小鸟>,我感触很深. 在书中我明白了很多,大 ...