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", "swf,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;
}
}
}

C# 结合html5 批量上传文件和图片预览的更多相关文章

  1. Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览

    1.FileUpload上传控件:(原文:http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUpload 控件显示 ...

  2. 【django】ajax,上传文件,图片预览

    1.ajax 概述: AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...

  3. atitit.javascript js 上传文件的本地预览

    atitit.javascript js 上传文件的本地预览 1. .URL.createObjectURL  1 1.1. 吊销所有使用 URL.createObjectURL 而创建的 URL,以 ...

  4. dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.

    http://wxb.github.io/dropzonejs.com.zh-CN/dropzonezh-CN/ 由于项目需要,完成一个web的图片拖拽上传,也就顺便学习和了解了一下前端的比较新的技术 ...

  5. DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.

    DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库. 它是轻量级的,不依赖任何其他类库(如JQuery)并且高度可定制. 试试看! 将文件拖至此处或点击上传.(这仅仅是 dropzo ...

  6. TP3.2批量上传文件(图片),解决同名冲突问题

    1.html <form action="{:U('Upload/index')}" enctype="multipart/form-data" meth ...

  7. html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器

    以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上 ...

  8. JavaScript图片上传前的图片预览功能

    JS代码: //js本地图片预览,兼容ie[6-9].火狐.Chrome17+.Opera11+.Maxthon3 function PreviewImage(fileObj, imgPreviewI ...

  9. html页面选择图片上传时实现图片预览功能

    实现效果如下图所示 只需要将下面的html部分的代码放入你的代码即可 (注意引入jQuery文件和html头部的css样式,使用的是ajax提交) <!-- 需引入jQuery 引入样式文件 引 ...

随机推荐

  1. Nginx详解二十六:Nginx架构篇之性能优化

    一.性能优化考虑点 1.当前系统结构瓶颈 通过压力测试观察指标.日志检测.性能分析 2.了解业务模式 接口业务类型.系统层次化结构 3.性能与安全 二.ab接口压力测试工具 1.安装:yum -y i ...

  2. easyui 布局之window和panel一起使用时,拉动window宽高时panel不跟随一起变化

    项目开发中布局是每一个组件都由最外层的window和内部的至少一个panel组成,其他的细小组件再依次放到panel中. 问题:当拉动外部的window时我们希望内部的panel的宽高也跟着变化,但是 ...

  3. IDEA中tomcat的部署

    创建一个项目就要部署tomcat

  4. 20165206 2017-2018-2 《Java程序设计》第三周学习总结

    20165206 2017-2018-2 <Java程序设计>第三周学习总结 教材学习内容总结 类:class是关键字,用来定义类. 类声明:例如class People. 对象的声明:类 ...

  5. 腾讯云Ubuntu安装可视化桌面

    1.安装图形界面 sudo apt-get update 更新 1).sudo apt-get install xinit 2).sudo apt-get install gdm  ( 登陆窗口,用于 ...

  6. WPF 对控件进行截图且不丢失范围(转载)

    原文:Taking WPF “Screenshots” I was recently working on a Surface project at Microsoft (that will be s ...

  7. Nodejs通过账号密码连接MongoDB数据库

    转自https://blog.csdn.net/szu_lzz/article/details/77435804#commentBox 1.创建管理员 首先开启Mongo服务,然后切换admin数据库 ...

  8. redis中实现redis-cli任意目录执行

  9. 使用spark集成kudu做DDL

    spark对kudu表的创建 定义kudu的表需要分成5个步骤: 1:提供表名 2:提供schema 3:提供主键 4:定义重要选项:例如:定义分区的schema 5:调用create Table a ...

  10. eclipse启动web应用 报错

    错误:The origin server did not find a current representation for the target resource or is not willing ...