asp.net MVC简单图片上传

asp.net MVC简单图片上传asp.netMVC简单图片上传01、创建控制器HomeController.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;namespacemvcTuPianShangChuang.Controllers{publicclassHomeController:Controller{

大家好,又见面了,我是你们的朋友全栈君。

asp.net MVC简单图片上传

01、创建控制器HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace mvcTuPianShangChuang.Controllers
{ 
   
    public class HomeController : Controller
    { 
   
        // GET: Home
        public ActionResult Index()
        { 
   
            return View();
        }

        [HttpPost]
        public ActionResult UploadImg()

        { 
   
            //接收表单传递过来的图片
            HttpPostedFileBase file = Request.Files["imagesFile"];


            //拿到文件的扩展名
            string extName = System.IO.Path.GetExtension(file.FileName);
            if (extName != ".jpeg" && extName != ".gif" && extName != ".jpg" && extName != ".png")
            { 
   
                //返回前一页
                return Content("<script>alert('文件格式不合适,请重新上传');history.go(-1);</script>");
            }
            //最终上传路径
            string uploadImgName = "/Upload/" + Guid.NewGuid().ToString() + file.FileName;
            //将上传的图片保存
            file.SaveAs(Request.MapPath(uploadImgName));
            //返回前一页
            return Content("<script>alert('上传成功!');history.go(-1);</script>");
        }

        //[HttpPost]
        //public ActionResult UploadImg()
        //{ 
   
        // if (Request.Files.Count > 0)
        // { 
   
        // HttpPostedFileBase f = Request.Files["file1"];
        // f.SaveAs(@"D:\" + f.FileName);
        // }
        // return Content("<script>alert('上传成功!');history.go(-1);</script>");
        // //return View();
        //}

    }
}

02创建视图Index.cshtml

@{ 
   
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <form action="/home/uploadimg" method="post" enctype="multipart/form-data">
            <input type="file" name="imagesFile" />
            <input type="submit" value="submit" />
        </form>


    </div>
</body>
</html>

03创建文件夹Upload
在这里插入图片描述

04预览即可

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162895.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • datagrip 2021激活码【注册码】

    datagrip 2021激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    35
  • iphone尺寸大全对照表2021 iphone屏幕尺寸大全

    iphone尺寸大全对照表2021 iphone屏幕尺寸大全iPhone11 6.1英寸 iPhone11Pro 5.8英寸 iphone手机爆降价1500这活动太给力了机会不容错过http://iphone.adiannao.cn/2iPhone11ProMax 6.5英寸iPhoneSE2020款 4.7英寸 iPhone12mini 5.4英寸 iPhone12 6.1英寸 iPhone12Pro 6.1英寸 iPhone12ProMax 6.7英寸第一代iPhone2GiOS1

    2022年5月15日
    1.0K
  • URL转发基础!_URL怎么用

    URL转发基础!_URL怎么用 1、什么是URL转发?所谓URL转发是当你访问一个域名时,将会自动跳转到您所指定的另一个网络地址(URL)。假设abc.com是您要访问的域名,则通过URL转发服务可以实现当访问http://www.abc.com时,自动转向访问另外一个URL,如:http://www.otherdomain.com/somedir/other.htm。URL转发服务尤其对于拥有一个主网站并同时拥有多个域

    2022年10月10日
    0
  • 解决安装office2013时出现Microsoft setup bootstrapper已停止工作问题

    解决安装office2013时出现Microsoft setup bootstrapper已停止工作问题MicrosoftSetupBootstrapper已停止工作**问题出现背景:**不小心删除office2013安装后的文件导致office软件无法使用,重新安装时出现还问题。**问题解决方法:**首先,卸载老的office;然后再安装不再出现MicrosoftSetupBootstrapper已停止工作的问题。…

    2022年7月20日
    47
  • cms模板「建议收藏」

    cms模板「建议收藏」http://www.zhicheng.com/

    2022年10月9日
    1
  • 说说anchorPoint[通俗易懂]

    说说anchorPoint[通俗易懂]anchorPoint属性是CGPoint(x,y),x,y的取值是按比例取值,一般用0~1,默认是(0.5,0.5),表示图层的position在自身的位置,举个例子,在红色view确定完大小位

    2022年7月3日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号