asp.net的文件上传都是单个文件上传方式,无法执行一次性多张图片批量上传操作,要实现多图片批量上传需要借助于flash,通过flash选取多个图片(文件),然后再通过后端服务进行上传操作.本次教程所使用的flash上传文件是 swfupload,下面会有源码下载链接。使用工具 ...
get='_blank'>asp.net的文件上传都是单个文件上传方式,无法执行一次性多张图片批量上传操作,要实现多图片批量上传需要借助于flash,通过flash选取多个图片(文件),然后再通过后端服务进行上传操作.
本次教程所使用的flash上传文件是 swfupload,下面会有源码下载链接。
使用工具 vs 2010。
演示效果图
第一步 新建一个web项目
第二步 引入所需swfuplod文件(swfupload.swf,js,css等)
第三步 新建一个一般处理程序(upload.ashx)
upload.ashx程序文件代码
using System;using System.Collections.Generic;using System.Web;using System.IO; namespace yuyue.upload{ /// <summary> /// upload 的摘要说明 /// </summary> public class upload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; try { HttpPostedFile file; for (int i = 0; i < context.Request.Files.Count; ++i) { file = context.Request.Files[i]; if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue; string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + RndNumStr(6) + Path.GetExtension(file.FileName); //文件名=上传日期+随机字符串+扩展名(可避免多人上传是第一名问题) /********************文件夹**************************/ string year=DateTime.Now.Year.ToString(); string monthday = DateTime.Now.ToString("MMdd"); if (!Directory.Exists(HttpContext.Current.Server.MapPath("/uploads/")+year)) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/uploads/") + year); } if (!Directory.Exists(HttpContext.Current.Server.MapPath("/uploads/") + year + "/" + monthday)) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/uploads/") + year + "/" + monthday); } file.SaveAs(HttpContext.Current.Server.MapPath("/uploads/" + year + "/" + monthday + "/" + filename)); context.Response.Write("/uploads/" + year + "/" + monthday + "/" + filename);//输出上传地址以用于前台js获取 } } catch (Exception ex) { context.Response.StatusCode = 500; context.Response.Write(ex.Message); context.Response.End(); } finally { context.Response.End(); } } #region 该方法用于生成指定位数的随机字符串 /// <summary> /// 该方法用于生成指定位数的随机字符串 /// </summary> /// <param name="VcodeNum">参数是随机数的位数</param> /// <returns>返回一个随机数字符串</returns> public static string RndNumStr(int VcodeNum) { string[] source = { "0", "1", "1", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; string checkCode = String.Empty; Random random = new Random(); for (int i = 0; i < VcodeNum; i++) { checkCode += source[random.Next(0, source.Length)]; } return checkCode; } #endregion public bool IsReusable { get { return false; } } }}
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:asp.net+swfupload 多图片批量上传(附源码下载)
关键词:ASP.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。