C# 结合html5 批量上传文件和图片预览
发布时间:2020-12-15 17:53:43 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 html5 新特性 input id="imgsf" type="file" name="imgsf" multiple /input file 中增加 multiple 属性可以选择多文件。IE9以下版本不兼容 form id="f
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 html5 新特性 <input id="imgsf" type="file" name="imgsf" multiple /> input file 中增加 multiple 属性可以选择多文件。IE9以下版本不兼容 <form id="form1" method="post" action="upload_json.ashx" enctype="multipart/form-data"> <div> <input id="imgsf" type="file" name="imgsf" multiple /> <input type="text" name="ceshi" value="panlitao" > <input type="submit" value="提交" /> </div> </form> //预览js <div id="imgrq"> </div> <script type="text/javascript"> $("#imgsf").change(function () { var filedx = 0; for (var i = 0,j = this.files.length; i < j; i++) { $("#imgrq").append("<img src="" + window.URL.createObjectURL(this.files[i]) + "" width="100" height="100" />"); } }); </script> C# 代码 public class upload_json : IHttpHandler { // private HttpContext context; public void ProcessRequest(HttpContext context) { String aspxUrl = context.Request.Path.Substring(0,context.Request.Path.LastIndexOf("/") + 1); //文件保存目录路径 String savePath = "attached/"; //文件保存目录URL String saveUrl = aspxUrl + "attached/"; //定义允许上传的文件扩展名 Hashtable extTable = new Hashtable(); extTable.Add("image","gif,jpg,jpeg,png,bmp"); //extTable.Add("flash","swf,flv"); //extTable.Add("media",flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"); //extTable.Add("file","doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"); //最大文件大小 int maxSize = 1000000; // this.context = context; String newFileName = ""; for (int i = 0; i < context.Request.Files.Count;i++ ) { HttpPostedFile imgFile = context.Request.Files[i]; if (imgFile == null) { showError("请选择文件。"); } String dirPath = context.Server.MapPath(savePath); if (!Directory.Exists(dirPath)) { showError("上传目录不存在。"); } String dirName = context.Request.QueryString["dir"]; if (String.IsNullOrEmpty(dirName)) { dirName = "image"; } if (!extTable.ContainsKey(dirName)) { showError("目录名不正确。"); } String fileName = imgFile.FileName; String fileExt = Path.GetExtension(fileName).ToLower(); if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize) { showError("上传文件大小超过限制。"); } //修改为文件流去判断文件格式 // string exPath = mall_bll.Common.isfilltype(stream: imgFile.InputStream).ToLower(); // if (exPath != "jpg" && exPath != "gif" && exPath != "bmp" && exPath != "png") // { showError("上传文件扩展名是不允许的扩展名。n只允许" + ((String)extTable[dirName]) + "格式。"); } //if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','),fileExt.Substring(1).ToLower()) == -1) //{ // showError("上传文件扩展名是不允许的扩展名。n只允许" + ((String)extTable[dirName]) + "格式。"); //} //创建文件夹 dirPath += dirName + "/"; saveUrl += dirName + "/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } String ymd = DateTime.Now.ToString("yyyyMMdd",DateTimeFormatInfo.InvariantInfo); dirPath += ymd + "/"; saveUrl += ymd + "/"; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff",DateTimeFormatInfo.InvariantInfo) + fileExt; String filePath = dirPath + newFileName; imgFile.SaveAs(filePath); } String fileUrl = saveUrl + newFileName; Hashtable hash = new Hashtable(); hash["error"] = 0; hash["url"] = fileUrl; context.Response.AddHeader("Content-Type","text/html; charset=UTF-8"); // context.Response.Write(JsonMapper.ToJson(hash)); context.Response.End(); } private void showError(string message) { Hashtable hash = new Hashtable(); hash["error"] = 1; hash["message"] = message; // context.Response.AddHeader("Content-Type","text/html; charset=UTF-8"); // context.Response.Write(JsonMapper.ToJson(hash)); // context.Response.End(); } public bool IsReusable { get { return true; } } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |