1.样式

2.js

3.后台处理

4.效果图

一.样式

<style>
.divUpload {
position: relative;
} .divUploadFirst {
width: 100px;
height: 36px;
background: #666666;
color: #fff;
text-align: center;
line-height: 36px;
} .file_input {
width: 200px; /*因为file-input在部分浏览器中会自带一个输入框,需要双击才可以点击上传,放大后将其定位到div外面就好啦*/
height: 36px;
position: absolute;
left: -100px;
top: 0;
z-index: 1;
-moz-opacity: 0;
-ms-opacity: 0;
-webkit-opacity: 0;
opacity: 0; /*css属性——opcity不透明度,取值0-1*/
filter: alpha(opacity=0); /*兼容IE8及以下--filter属性是IE特有的,它还有很多其它滤镜效果,而filter: alpha(opacity=0); 兼容IE8及以下的IE浏览器(如果你的电脑IE是8以下的版本,使用某些效果是可能会有一个允许ActiveX的提示,注意点一下就ok啦)*/
cursor: pointer;
}
</style>
  <div class="divUpload">
<div class="divUploadFirst" style="width:82px;height:34px;border-radius:14px;">上传协议</div>
<input type="file" class="file_input" id="txtUpload" onchange="upload()" accept="image/*" multiple >
</div>
  <div class="form-group" style="clear:both">
<div id="images">
</div>
</div>

二 JS

 //上传协议
function upload() {
var uploaderUrl = '@Url.Content("~/PetCase/Upload")';
var files = document.getElementById("txtUpload").files;
var fd = new FormData();
for (var i = ; i < files.length; i++) {
fd.append("file["+i+"]", files[i]);
}
fd.append('caseNum', caseNum);
$.ajax({
url:uploaderUrl,
type :"post",
data:fd,
dataType: "json",
cache: false,//上传文件无需缓存
processData: false,//用于对data参数进行序列化处理 这里必须false
contentType: false,
success:function(data){
if(data.length >){
for (var i = ; i < data.length; i++) {
imgPath.push(data[i].imgpath); var html = '<div style="width:200px;float:left;margin-left: 5px;cursor:pointer"> <div class="sh" ></div><div class="img1"><img src="' + data[i].imgpath + '" style="width:200px;" /></div></div>'
$("#images").append(html);
}
addPichtml();
}
},
error:function(){
layer.msg('上传发生错误.');
}
});
}
function addPichtml() {
$(".sh").bind('click', function () {
var $img = $(this).parent().find("img");
var ipath = $img.attr("src");
for (var i = ; i < imgPath.length; i++) {
if (imgPath[i] == ipath) {
imgPath.splice(i, );
$(this).parent().remove();
DelImgPath(ipath);
}
}
}) $(".img1").bind('click', function () {
var $img = $(this).parent().find("img");
var ipath = $img.attr("src");
for (var i = ; i < imgPath.length; i++) {
if (imgPath[i] == ipath) {
layer.open({
type: ,
title: "协议",
content: '<div><img src="' + ipath + '" /></div>',
maxmin: true,
area: [ + "px", + "px"]
})
}
}
}) }

三 后台处理

[HttpPost]
public ActionResult Upload(HttpPostedFileBase Filedata, string caseNum)
{
// 没有文件上传,直接返回
//if (Filedata == null || string.IsNullOrEmpty(Filedata.FileName) || Filedata.ContentLength == 0)
//{
// return HttpNotFound();
//} #region 另外一种方式上传
if (Request.Files == null || Request.Files.Count == )
{
return HttpNotFound();
} List<object> result = new List<object>();
for (int i = ; i < Request.Files.Count; i++)
{
Filedata = Request.Files[i];
#endregion string FileEextension = Path.GetExtension(Filedata.FileName);
string uploadYear = DateTime.Now.Year.ToString();
string uploadDate = DateTime.Now.Date.ToString("MMdd"); // string dirpath = System.Configuration.ConfigurationManager.AppSettings["WebTransferHospitalImgDirPath"];
string filepath = System.Configuration.ConfigurationManager.AppSettings["UploadProtocolPath"];
string strGUID = System.Guid.NewGuid().ToString();
strGUID = DateTime.Now.ToString("yyyMMddHHmmssfffffff")+Guid.NewGuid();//上传的图片按上传时间的先后来命名,显示再按命名来排序 string fullFileName = Server.MapPath("~/UploadFile/UploadProtocol") + "/" + uploadYear + "/" + uploadDate + "/" + caseNum + "/" + strGUID + FileEextension;
string webPath = filepath + "/" + uploadYear + "/" + uploadDate + "/" + caseNum + "/" + strGUID + FileEextension; //创建文件夹,保存文件
string path = Path.GetDirectoryName(fullFileName);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
} Filedata.SaveAs(fullFileName); var data = new { imgpath = webPath };
result.Add(data);
} return Json(result, JsonRequestBehavior.AllowGet);
}

四 效果图

上传文件,不依赖 Jquery flash 插件,用到HTML5 input 新属性实现过滤文件格式、同时上传多个文件的更多相关文章

  1. web 表单方式上传文件方法(不用flash插件)

    原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...

  2. HTML5 input date属性引起的探索——My97DatePicker(日期选择插件)

    不得不说H5的input date属性真的好用,之前我写的http://www.cnblogs.com/tu-0718/p/6729274.html这篇博客里面也有提到,不过虽然移动端对H5的支持还是 ...

  3. 使用jQuery.form插件,实现完美的表单异步提交

    传送门:异步编程系列目录…… 时间真快,转眼一个月快结束了,一个月没写博客了!手开始生了,怎么开始呢…… 示例下载:使用jQuery.form插件,实现完美的表单异步提交.rar 月份的尾巴,今天的主 ...

  4. 分享一款超棒的jQuery旋钮插件 - jQuery knob

    转自:http://www.cnblogs.com/gbin1/archive/2012/05/08/2489908.html 在线演示  本地下载 如果你也在寻找一款生成漂亮旋钮(knob)的jQu ...

  5. jquery uploadify插件多文件上传

    1.jquery uploadify 下载:http://www.uploadify.com/ 2.安装:解压后拷贝的工程目录下面,如:WebRoot/uploaddify 3.配置项说明: uplo ...

  6. jquery 实现ajax 上传文件的功能(使用插件 ajaxfileupload)

    js文件的下载地址 : http://files.cnblogs.com/wangqc/ajaxfileupload.js 页面代码: <html>    <!-- 引入相关的js文 ...

  7. 使用jQuery.FileUpload插件和服Backload组件自定义上传文件夹

    在零配置情况下,文件的上传文件夹是根目录下的Files文件夹,如何自定义文件的上传文件夹呢? □ 在web.config中配置 1: <configuration> 2: <conf ...

  8. 利用Jquery使用HTML5的FormData属性实现对文件的上传

    1.利用Jquery使用HTML5的FormData属性实现对文件的上传 在HTML5以前我们如果需要实现文件上传服务器等功能的时候,有时候我们不得不依赖于FLASH去实现,而在HTML5到来之后,我 ...

  9. Uploadify & jQuery.imgAreaSelect 插件实现图片上传裁剪

    在网站中需要一个图片上传裁剪的功能,借鉴这篇文章 Ajax+PHP+jQuery图片截图上传 的指点,找到了jquery.imgAreaSelect这个不错插件,能对图片进行自定义区域选择并给出坐标, ...

随机推荐

  1. SQL Server: create table sql script

    ---摇奖observeh数据库设计 Function getSpace lottery /* -- Author:geovindu 涂聚文 -- Date: 20180427 为了自写生成代码.根据 ...

  2. [VUE ERROR] Invalid default value for prop "slides": Props with type Object/Array must use a factory function to return the default value

    错误原因: 当给子组件设置 props 属性时,如果参数类型是 Array 或 Object ,它的默认值必须是由工场函数返回,不能直接赋值 错误代码: proE: { type: Array, de ...

  3. Android SDK Manager无法更新的解决方案

    Android SDK Manager -> Tools -> Options HTTP Proxy Server:mirrors.neusoft.edu.cn HTTP Proxy Po ...

  4. Angular基础(五) 内建指令和表单

    ​ Angular提供了一些内建的指令,可以作为属性添加给HTML元素,以动态控制其行为. 一.内建指令 a) *ngIf,可以根据条件来显示或隐藏HTML元素. <div *ngIf='a&g ...

  5. Android中使用progurad混淆代码

    第一步,取消project.properties中关于progurad的注释,开启progurad,默认的配置文件会被加载进来. proguard.config=${sdk.dir}/tools/pr ...

  6. 转 fiddler常见的应用场景

    fiddler常见的应用场景   在移动互联网时代,作为软件测试工程师,fiddler绝对是值得掌握并添加进技术栈里的工具之一. 那么,fiddler在日常的测试工作中,一般都有哪些常见的应用场景呢? ...

  7. 模拟开户接口,使用shell脚本实现批量用户开通

    1.目的 通过模拟接口方法,实现批量用户开通 2.分析 A.接口含body和head部分,其中body中的某些变量为必填字段,包含用户的信息,接口可整理成body.xml.head.xml文件. B. ...

  8. C# 生成强命名程序集并添加到GAC

    针对一些类库项目或用户控件项目(一般来说,这类项目最后编译生成的是一个或多个dll文件),在程序开发完成后,有时需要将开发的程序集(dll文件)安装部署到GAC(全局程序集缓存)中,以便其他的程序也可 ...

  9. 启动OpenVPN失败

    启动OpenVPN失败 文:铁乐与猫 [root@yunwei_OpenVPN openvpn]# systemctl status openvpn ● openvpn.service - LSB: ...

  10. 无需软件windows如何加密文件夹

    在百部百科上看到,放在博客中以便查看. 1.首先打开记事本,当然如果你的电脑里装有类似notepad++的文本编辑软件的也可以,但是不能用word.用这类软件好处是代码高亮,看上去舒服,减少错误率. ...