上传使用ASP.NET MVC的图像 [英] Uploading an image using ASP.NET MVC

查看:128
本文介绍了上传使用ASP.NET MVC的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传图片作为在MVC应用程序创建操作的一部分。

的图像将被存储在文件服务器和数据库将包含该路径

我打算使用follwing标签来获取文件:


 > <输入类型=文件ID =MYIMAGENAME =MyImageName/>


我如何访问和保存该控制器的动作?


解决方案

我把这个在BaseController类,从中我所有的控制器继承:

  //这只是日期时间prefixes为YYYYMMDDHHMMSS的文件名,以
    //是使用该不会发生名称冲突。
    受保护的静态字符串prefixFName(字符串FNAME)
    {
        如果(String.IsNullOrEmpty(FNAME))
        {
            返回null;
        }
        其他
        {
            返回的String.Format({0} {1},
                                 DateTime.Now.ToString(YYYYMMDDHHMMSS),
                                 FNAME);
        }
    }    保护字符串SAVEFILE(HttpPostedFileBase文件,字符串路径)
    {
        如果(文件= NULL&放大器;!&安培; file.ContentLength大于0)
        {
            如果(路径== NULL)
            {
                抛出新的ArgumentNullException(路径不能为空);
            }
            字符串relpath =的String.Format({0} / {1},路径,prefixFName(file.FileName));
            尝试
            {
                file.SaveAs(使用Server.Mappath(relpath));
                返回relpath;
            }
            赶上(HttpException E)
            {
                抛出新ApplicationException的(无法保存上传的文件,E);
            }
        }
        返回null;
    }

然后,控制器我做的:

  savedPath = SAVEFILE(Request.Files [标志],somepath);

I need to upload an image as part of a create action in an MVC application.

The image will be stored in the Files server and the db will contain a path to that.

I plan to use the follwing tag to get the file:

> <input type="file" id="MyImage" name="MyImageName" />

How do I access and save this in the controller action?

解决方案

I put this in a BaseController class, from which all my controllers inherit:

    // this just prefixes datetime as yyyyMMddhhmmss to the filename, to
    // be use that no name collision will occur.
    protected static String PrefixFName(String fname)
    {
        if (String.IsNullOrEmpty(fname))
        {
            return null;
        }
        else
        {
            return String.Format("{0}{1}",
                                 DateTime.Now.ToString("yyyyMMddhhmmss"),
                                 fname);
        }
    }

    protected String SaveFile(HttpPostedFileBase file, String path)
    {
        if (file != null && file.ContentLength > 0)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path cannot be null");
            }
            String relpath = String.Format("{0}/{1}", path, PrefixFName(file.FileName));
            try
            {
                file.SaveAs(Server.MapPath(relpath));
                return relpath;
            }
            catch (HttpException e)
            {
                throw new ApplicationException("Cannot save uploaded file", e);
            }
        }
        return null;
    }

Then, in the controller I do:

savedPath = SaveFile(Request.Files["logo"], somepath);

这篇关于上传使用ASP.NET MVC的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆