• 加强版上传图片功能啊


    一般处理程序

    filename


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

    namespace EFDemo
    {
        /// <summary>
        /// FileUpload 的摘要说明
        /// </summary>
        public class FileUpload : IHttpHandler
        {

            public void ProcessRequest(HttpContext context)
            {
                var file = context.Request.Files["file"];
                var filename = "/Img/" + file.FileName;
                file.SaveAs(context.Server.MapPath("~/" + filename));
                context.Response.Write(filename);
            }

            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }


    ImgController

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

    namespace EFDemo.Controllers
    {
        public class ImgController : Controller
        {
            // GET: Img
            public ActionResult Index()
            {
                return View();
            }

            public void ImgUpload()
            {
                var file = Request.Files["fileImg"];

              
            }
        }
    }
     
     
    @{
        ViewBag.Title = "Index";
    }

    <h2>Index</h2>

    <style>
        .img {
             80px;
            height: 80px;
            margin:10px;
        }
    </style>

    <div>
        @*<form id="imgfrom" method="post" enctype="multipart/form-data" action="~/FileUpload.ashx">
            </form>*@
        <input type="file" name="fileImg" id="fileImg" />

        <input type="button" value="上传" onclick="upload()" />
    </div>
    <div id="imgs">

    </div>

    <script src="~/Scripts/jquery-3.4.1.min.js"></script>

    <script>

        function upload() {

            var formData = new FormData();
            formData.append("file", $("#fileImg")[0].files[0]);

            $.ajax({
                url: '/FileUpload.ashx',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function (responseStr) {
                    alert("上传成功");

                    var html = "<img   src='" + responseStr + "' class='img' />";

                    $("#imgs").append(html);

                }
            });


        }

    </script>
     
     
  • 相关阅读:
    go学习-环境安装1-WIN10如何安装wsl2
    go学习-环境安装2-wsl2安装docker
    go学习-使用swagger生成接口文档
    go学习-WSL安装gcc
    go学习-如何修改Docker0的IP地址的默认网段
    go学习-go-sqlmock数据库操作测试
    go学习-环境安装3-wsl安装golang
    java基础学习-Stream API
    go学习-gorm
    go学习-获取form表单提交数据
  • 原文地址:https://www.cnblogs.com/gc1229/p/13150364.html
Copyright © 2020-2023  润新知