ASP.NET+LayUI实现多图片上传(ashx)

    xiaoxiao2022-07-07  224

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="HighGrade.aspx.cs" Inherits="HighGrade" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="layui-v2.4.5/layui/css/layui.css" rel="stylesheet" /> </head> <body> <div class="layui-upload"> <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button> <div class="layui-upload-list"> <table class="layui-table"> <thead> <tr> <th>文件名</th> <th>大小</th> <th>状态</th> <th>操作</th> </tr> </thead> <tbody id="demoList"></tbody> </table> </div> <button type="button" class="layui-btn" id="testListAction" onclick="noFile()">开始上传</button> </div> <script src="layui-v2.4.5/layui/layui.js"></script> <script src="jquery-1.8.3/jquery.js"></script> <script> layui.use('upload', function () { var upload = layui.upload; var demoListView = $('#demoList') ,uploadListIns = upload.render({ elem: '#testList' , url: 'Handler.ashx' ,accept: 'file' , multiple: true , size: 30000 ,auto: false ,bindAction: '#testListAction' ,choose: function(obj){ var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列 //读取本地文件 obj.preview(function(index, file, result){ var tr = $(['<tr id="upload-'+ index +'">' ,'<td>'+ file.name +'</td>' ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>' ,'<td>等待上传</td>' ,'<td>' ,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>' ,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>' ,'</td>' ,'</tr>'].join('')); //单个重传 tr.find('.demo-reload').on('click', function(){ obj.upload(index, file); }); //删除 tr.find('.demo-delete').on('click', function(){ delete files[index]; //删除对应的文件 tr.remove(); uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选 }); demoListView.append(tr); }); } , done: function (res, index, upload) { //alert(res); //if(res.code == false){ //上传成功 // var tr = demoListView.find('tr#upload-'+ index) // ,tds = tr.children(); // tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>'); // tds.eq(3).html(''); //清空操作 // return delete this.files[index]; //删除文件队列已经上传成功的文件 //} //this.error(index, upload); if (res.code === "200") { var tr = demoListView.find('tr#upload-'+ index) tds = tr.children(); tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>'); tds.eq(3).html(''); //清空操作 return delete this.files[index]; //删除文件队列已经上传成功的文件 } } ,error: function(index, upload){ var tr = demoListView.find('tr#upload-'+ index) ,tds = tr.children(); tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>'); tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传 } }); }) function noFile() { var content = $("#demoList").html(); if (content == null || content == 0) { layer.alert("请选择文件") } } </script> </body> </html> <%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; using System.Collections.Generic; using System.IO; using System.Linq; using System.Collections; public class Handler : IHttpHandler { //public void ProcessRequest (HttpContext context) { // context.Response.ContentType = "text/plain"; // // context.Response.Write("Hello World"); // string cmd = context.Request["cmd"]; // switch (cmd) // { // case "UploadSomeFiles": // { // UploadSomeFiles(context); // break; // } // } //} //private void UploadSomeFiles(HttpContext context) //{ // string path = ""; // string name = ""; // try // { // int count = context.Request.Files.Count; // for (int i = 0; i < count; i++) // { // int contentLength = context.Request.Files[i].ContentLength;//文件的大小 // string contentType = context.Request.Files[i].ContentType;//文件的类型 // string localPath = context.Request.Files[i].FileName;//文件的本地路径 // string extension = Path.GetExtension(localPath).ToLower();//文件的后缀 // string oldName = Path.GetFileName(localPath); // string newName = DateTime.Now.ToString("yyyyMMddHHmm_") + oldName.Replace(" ", "_").Replace("&", "_"); // string filePath = GetPathForSaveFolder(); // string fileSavePath = HttpContext.Current.Server.MapPath(filePath); // fileSavePath = fileSavePath + newName; // context.Request.Files[i].SaveAs(fileSavePath); // path = filePath + newName; // name = oldName; // } // } // catch (Exception ex) // { // context.Response.Write("{\"isSuccess\":\"false\",\"url\":\"上传失败\"}"); // } // finally // { // context.Response.Write("{\"isSuccess\":\"true\",\"url\":\"" + path + "\",\"name\":\"" + name + "\"}"); // } //} //private string GetPathForSaveFolder() //{ // string path = GetAppPath() + "UploadFile/items/" + DateTime.Now.ToString("yyyy-MM") + "/"; // string fileSavePath = HttpContext.Current.Server.MapPath(path); // if (!Directory.Exists(fileSavePath)) // { // Directory.CreateDirectory(fileSavePath); // } // return path; //} //private string GetAppPath() //{ // string applicationPath = HttpContext.Current.Request.ApplicationPath; // if (!applicationPath.EndsWith("/")) // { // applicationPath = applicationPath + "/"; // } // return applicationPath; //} public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { if (true) { context.Response.ContentType = "text/plain"; result result = new result(); string fileNewName = string.Empty; string filePath = string.Empty; HttpFileCollection files = context.Request.Files; if (files.Count > 0) { ArrayList alist = new ArrayList(); int count = context.Request.Files.Count; string ImgUrl = string.Empty; string VendorPid = string.Empty; for (int i = 0; i < count; i++) { //设置文件名 fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(files[0].FileName); //保存文件 files[0].SaveAs(context.Server.MapPath("~/Files/" + fileNewName)); //result.code = "200"; //result.msg = "文件上传成功!"; ImgUrl = "~/Files/" + fileNewName; VendorPid = fileNewName.Substring(17, 18); String sql = @"update test_img set imgurl='" + ImgUrl + "' where vendorpid='" + VendorPid + "'"; alist.Add(sql); } if (alist.Count > 0) { count = DbHelperOra.ExecuteSqlTran(DbHelperOra.conlodb, alist); } if (count > 0) { result.code = "200"; result.msg = "文件上传成功!"; } } else { result.code = "200"; result.msg = "文件上传失败!"; } context.Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(result)); context.Response.End(); } } public class result { public string code { get; set; } public string msg { get; set; } } }

     

    最新回复(0)