C# .Net单图上传,多图上传源码Demo

此图片上传是由C# .NET技术写的一个完整的单张图片上传或多张图片上传的Demo,简单易懂,操作简单,功能强大。

html页面:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8"/>
    <script src="scripts/jquery-3.1.1.min.js"></script>
    <script src="scripts/jquery.form.js"></script>
    <script>
        function upload(formid) {
            $("#" + formid).ajaxSubmit({
                url: "Handler.ashx?action=upload_img",
                type: "POST",
                dataType: "json",
                clearForm: true,
                resetForm: true,
                success: function (e) {
                    if (e.status == 1) {
                        var temp = "";
                        for (var i = 0; i < e.files.length; i++) {
                            temp += "<image src='"+e.files[i].path+"' alt='"+e.files[i].filename+"' />";
                        }
                        $("#div_result").before(temp);
                        $("#div_result").text(JSON.stringify(e));
                    }
                },
                error: function (e) {
                    alert(e.statusText);
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" method="POST" enctype="multipart/form-data" action="#" data-ajax="false">
        <label for="file">多文件上传</label>
        <input type="file" id="file" name="image" accept="image/png,image/jpg,image/bmp,image/jpeg" multiple="multiple" style="display: none" onchange="return upload('form1');"/>
        <div id="div_result"></div>
    </form>
</body>
</html>

一般处理程序:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Test
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["action"];
            switch (action)
            {
                case "upload_img":
                    upload_img(context);
                    break;
            }
        }

        private void upload_img(HttpContext context)
        {
            HttpFileCollection files = context.Request.Files;
            IList<HttpPostedFile> list = files.GetMultiple("image");
            JObject json = new JObject();
            json["status"] = 1;
            JArray jfiles = new JArray();
            //文件上传相对路径
            string path = "/upload/";
            //获取绝对路径
            string mappath = context.Server.MapPath(path);
            if (!Directory.Exists(mappath))
            {
                Directory.CreateDirectory(mappath);
            }
            foreach (HttpPostedFile file in list)
            {
                //获取扩展名
                string ext = Path.GetExtension(file.FileName);
                //生成新的文件名
                string name = Guid.NewGuid().ToString("N");
                //加上扩展名
                string newfile = name + ext;
                //保存上传的文件
                file.SaveAs(mappath + newfile);
                JToken jfile = new JObject();
                jfile["ext"] = ext;
                jfile["filename"] = newfile;
                jfile["path"] = path + newfile;
                jfiles.Add(jfile);
            }
            json["files"] = jfiles;
            context.Response.Write(json.ToString());
        }

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

猜你喜欢

转载自blog.csdn.net/IT_0802/article/details/86672527
今日推荐