zoukankan      html  css  js  c++  java
  • ASP.NET MVC上传图片并把图片保存在本地

    一、上传单张图片

    前端

    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            @using (Html.BeginForm("addFile", "First", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <input type="file" name="myFile" />
                <input type="submit" />
            }
        </div>
    </body>
    </html>

    后台

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace 上传图片并把图片保存在本地.Controllers
    {
        public class FirstController : Controller
        {
            public static string toImagePath = @"D:";
    
            // GET: First
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult addFile()
            {
                try
                {
                    HttpPostedFileBase file = Request.Files["myFile"];
    
                    string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
    
                    if (file.ContentType == "image/jpeg" || file.ContentType == "image/png")
                    {
                        if (int.Parse(file.ContentLength.ToString()) > ((1024 * 1024) * 5))
                        {
                            //图片大小不能大于5M;
                            return Redirect("/First/Index");
                        }
                        else
                        {
                            //图片存储路径
                            file.SaveAs(toImagePath + FileName + ".jpg");
                        }
                    }
                    return Redirect("/First/Index");
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }

     二、上传多张图片

    前端

    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div>
            @using (Html.BeginForm("addFile", "First", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <input type="file" multiple="multiple" name="myFile" />
                <input type="submit" />
            }
        </div>
    </body>
    </html>

    后台

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace 上传图片并保存在本地.Controllers
    {
        public class FirstController : Controller
        {
            public static string toImagePath = @"D:";
    
            // GET: First
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult addFile(IEnumerable<HttpPostedFileBase> myFile)
            {
                try
                {
                    foreach (var file in myFile)
                    {
                        if (file != null && file.ContentLength > 0)
                        {
                            if (file.ContentType == "image/jpeg" || file.ContentType == "image/png")
                            {
                                if (int.Parse(file.ContentLength.ToString()) > (1024 * 1024) * 5)
                                {
                                    //图片大小不能大于5M;
                                    continue;
                                }
                                else
                                {
                                    string FileName = DateTime.Now.ToString("yyyyMMddhhmmss");
                                    //图片存储路径
                                    file.SaveAs(toImagePath + FileName + ".jpg");
                                }
                            }
                        }
                    }
                    return Redirect("/First/Index");
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }

    检查是否要创建上传文件夹

            private bool CreateFolderIfNeeded(string path)
            {
                bool result = true;
    
                if (!Directory.Exists(path))
                {
                    try
                    {
                Directory.CreateDirectory(path);
    return result; } catch (Exception) { result = false; } } return result; }

      如何把图片存储到当前项目某个文件夹

    string urlPath = Server.MapPath(toImagePath);// 读取到当前虚拟目录的根目录

      后续会陆续更新其他资料,喜欢请关注哦!

      我的博客:https://www.cnblogs.com/duhaoran/

  • 相关阅读:
    分布式爬虫
    前端页面展示
    fillter根据value来匹配字段
    element ui 怎么去修改el-date-picker的时间
    element ui,input框输入时enter健进行搜索
    element ui 里面的table怎么弹出一个框让表中数据点击出现弹框
    修改数据结构记录,将同级数据改成父子集数据
    h5的复制功能的使用,Clipboard.js的使用,主要是在app里面使用
    在安卓手机下按钮会悬浮在键盘上,怎么解决vue.js
    last-child为啥不生效
  • 原文地址:https://www.cnblogs.com/duhaoran/p/13645910.html
Copyright © 2011-2022 走看看