1、图片框样式与进度条样式

.alert_img_content { width: 44%; float: left; margin: 3%; border: 1px solid #ddd; background: url(/Content/images/jiahao.png) no-repeat 50%; position: relative; box-sizing: border-box; }
.alert_img_content input { display: block; height: 100%; width: 100%; opacity: 0; position: absolute; top: 0; left: 0; z-index: 2; }
.alert_img_content img { display: block; height: 100%; width: 100%; position: absolute; top: 0; left: 0; z-index: 1; }
.alert_img_content a { display: block; text-align: right; padding-right: 7px; padding-top: 2px; font-size: 20px; box-sizing: border-box; border-bottom-left-radius: 100%; height: 40px; width: 40px; background-color: red; color: #fff; /* line-height: 23px; */ position: absolute; top: 0; right: 0; z-index: 3; }
.clearfix:after { content: ""; display: block; height: 0; clear: both; visibility: hidden; }
.progress-bar { background-color: #666; width: 100%; text-align: center; position: relative; height: 20px; line-height: 20px; border-radius: 10px; }
.progress-bar-font { width: 100%; color: #fff; position: absolute; z-index: 3; }
.progress-bar-plan { background-color: red; position: absolute; height: 20px; width: 0%; border-radius: 10px; transition: 0.3s; }

2、图片标签

<div class="alert_img_box clearfix">
<div class="alert_img_content">
<input type="file" accept="image/*" capture="camera" onchange="inputChange(this)">
<img />
<a style="display: none;" onclick="deletePic(this)">×</a>
</div>
</div>

3、进度条标签

<div class="progress-bar" style="display: none;">
<div class="progress-bar-plan"></div>
<div class="progress-bar-font">
0%
</div>
</div>

4、file绑定的方法

function inputChange(obj)
{
var picList = $(".alert_img_content"); var pic = picList.eq(picList.length - 1); if (pic.find("input").val() != "" && picList.length < 5)
{
$(".alert_img_box").append(html);
calcHeight();
} if ($(obj).val() != "")
{
$(obj).parent().find("a").show();
} //设置显示图片
function getObjectURL(file)
{
var url = null;
if (window.createObjcectURL != undefined)
{
url = window.createOjcectURL(file);
} else if (window.URL != undefined)
{
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined)
{
url = window.webkitURL.createObjectURL(file);
}
return url;
}
var objURL = getObjectURL(obj.files[0]); $(obj).parent().find("img").attr("src", objURL);
}

  5、删除代码

function deletePic(obj)
{
var len = $(".alert_img_content").length;
var pic = $(".alert_img_content").eq(len - 1); $(obj).parent().remove(); if (len == 5 && pic.find("input").val() != "")
{
$(".alert_img_box").append(html);
calcHeight();
}
}

6、保持图片框为正方形的代码

function calcHeight()
{
var obj = $(".alert_img_box .alert_img_content"); obj.height(obj.width());
}

7、上传的js代码

function saveDealinfo()
{
$("#btn_save").click(function ()
{
var formData = new FormData(); if ($("input[type='file']").length > 1)
{
$(".progress-bar").show();
$(".progress-bar-plan").css("background-color", "red"); $("input[type='file']").each(function (index, item)
{
formData.append("file", $("input[type='file']")[index].files[0]);
})
} formData.append("alarmID", "@Model.Item1.AlarmID");
formData.append("remark", $("textarea[name=remark]").val());
formData.append("alarmStatus", $("input[name=alarmDealStatus]:checked").val())
formData.append("alarmConfirm", $("input[name=alarmConfim]:checked").val()) //首先封装一个方法 传入一个监听函数 返回一个绑定了监听函数的XMLHttpRequest对象
var xhrOnProgress = function (fun)
{
xhrOnProgress.onprogress = fun; //绑定监听
//使用闭包实现监听绑
return function ()
{
//通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
var xhr = $.ajaxSettings.xhr();
//判断监听函数是否为函数
if (typeof xhrOnProgress.onprogress !== 'function')
return xhr;
//如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
if (xhrOnProgress.onprogress && xhr.upload)
{
xhr.upload.onprogress = xhrOnProgress.onprogress;
}
return xhr;
}
} $.ajax({
url: "/Interface/DealAlarm",
type: 'POST',
data: formData,
// 告诉jQuery不要去处理发送的数据
processData: false,
// 告诉jQuery不要去设置Content-Type请求头
contentType: false,
//beforeSend: function ()
//{
// console.log("正在进行,请稍候");
//},
xhr: xhrOnProgress(function (evt)
{
var percent = Math.floor(evt.loaded / evt.total * 100);//计算百分比 $(".progress-bar-plan").css("width", percent + "%");
$(".progress-bar-font").html(percent + '%'); if (percent == 100)
{
$(".progress-bar-plan").css("background-color", "#0bae27");
}
////console.log(percent);
//// 设置进度条样式
//$('#jdt').css('width',percent * 3 + 'px');
//$('#jdt').css('background','skyblue');
////显示进度百分比
//$('#jdt').text(percent+'%');
//$('#loaded').text(evt.loaded/1024 + 'K');
//$('#total').text(evt.total/1024 + 'K');
}),
success: function (response)
{
if (response.Code == 0)
{ }
else
{
}
},
error: function (responseStr)
{
console.log("error");
}
});
})
}

8、mvc的接收代码

public string DealAlarm()
{
ResponseResult<string> response = new ResponseResult<string>(); long alarmID = Convert.ToInt64(Request["alarmID"]);
string remark = Convert.ToString(Request["remark"]);
int alarmStatus = Convert.ToInt32(Request["alarmStatus"]);
int alarmConfirm = Convert.ToInt32(Request["alarmConfirm"]); List<string> urlList = new List<string>(); IResult iRst = SaveImage(urlList);
       
       return string.empty;
}

8、图片保存代码

public IResult SaveImage(List<string> urlList)
{
try
{
HttpFileCollectionBase coll = Request.Files;
HttpPostedFileBase file;
string domain = "//" + Request.Url.Host + (Request.Url.Port == ? "" : ":" + Request.Url.Port.ToString()) + "/Content/"; for (int i = ; i < coll.Count; i++)
{
file = coll[i]; if (string.IsNullOrEmpty(file.FileName))
{
continue;
} if (file.ContentLength == )
{
continue;
} string pictureName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
string extension = Path.GetExtension(file.FileName);
string folder = DateTime.Now.ToString("yyyyMM"); string path = PhotoUrl + "Upload\\" + folder + @"\" + pictureName + extension; if (!Directory.Exists(PhotoUrl + folder))
{
Directory.CreateDirectory(PhotoUrl + "Upload\\" + folder);
} file.SaveAs(path); urlList.Add(domain + folder + @"/" + pictureName + extension);
} return IResult.Success;
}
catch (Exception ex)
{
Logging.Logger.Error(ex);
return IResult.UnknownErr;
}
}

调用手机摄像头并上传图片--jquery ajax的更多相关文章

  1. html5调用手机摄像头,实现拍照上传功能

    今天做手机网站,想实现手机扫描二维码功能.首先实现在浏览器中调用手机摄像头,实现拍照功能并且把拍下的照片显示在页面并上传到服务器上,然后再在服务器端进行分析. 首先实现在浏览器中调用摄像头,当然用现在 ...

  2. 利用html5调用本地摄像头拍照上传图片[转]

    利用html5调用本地摄像头拍照上传图片   html5概念啥的就不废话了,不知道的 百度, 谷歌一堆..今天学了学html5中的Canvas结合新增的<video>标签来获取本地摄像头, ...

  3. 怎样让HTML5调用手机摄像头拍照——实践就是一切

    原文:怎样让HTML5调用手机摄像头拍照--实践就是一切 NanShan 小编将思路提供给了大家.学编程最重要的是实践,我这尽管有完好的代码,可是希望大家都能够自己写出属于自己的代码 HTML5 Th ...

  4. HTML5调用手机摄像头,仅仅支持OPPOHD浏览器

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 亲测可用)html5 file调用手机摄像头

    在切图网一个客户的webapp项目中需要用到 html5调用手机摄像头,找了很多资料,大都是 js调用api  然后怎样怎样,做了几个demo测试发现根本不行, 后来恍然大悟,用html5自带的 in ...

  6. 使用HTML5+调用手机摄像头和相册

    前言:前端时间使用HTML5做了一个WEB端APP,其中用到了H5页面调用手机摄像头的功能,当时也是花了不少时间去研究.最终是采用了HTML5plus(HTML5+)的方式完成了该功能,现将具体方法简 ...

  7. js 调用手机摄像头或相册并展示图片

    效果图 手机浏览器.微信打开该网页,都支持调用摄像头拍照和打开相册. 先看最终结果: 每次点击“点击上传”,可以选择相册或者拍照,选完以后可以多展示一张图片,同时上传服务器. 点击“重新上传”,清空所 ...

  8. 利用html5调用本地摄像头拍照上传图片

    这个是前台HTML的代码. <div id="contentHolder"> <video id="video" width="32 ...

  9. Android调用手机摄像头使用MediaRecorder录像并播放

    最近在项目开发中需要调用系统的摄像头录像并播放. 在开发中遇到了两个问题,记录下: (1)开发过程中出现摄像头占用,启动失败,报错.但是我已经在onDestory()中关闭了资源. 报错原因:打开程序 ...

随机推荐

  1. sublime 快捷键 【转】

    Sublime Text 3 快捷键精华版   备用,方便查询 Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+S ...

  2. GDAL利用地理坐标读取图像像元值

    最近的一个项目需要在电子海图中下载已知水深点,导出点的地理坐标(经纬度).然后在arcgis中打开这些地理坐标输出为shp,利用GDAL读取不同波段的点对应的像元值,从而构建水深和像元值的对应关系. ...

  3. php面试笔记(4)-php基础知识-流程控制

    本文是根据慕课网Jason老师的课程进行的PHP面试知识点总结和升华,如有侵权请联系我进行删除,email:guoyugygy@163.com 在面试中,考官往往喜欢基础扎实的面试者,而流程控制相关的 ...

  4. sparc v8 汇编语言语法

    1.3.1 Labeling Format Symbol names beginning with a dot (.) are assumed to be local symbols. Names b ...

  5. OSPF RFC2740

    2.5. Use of link-local addresses IPv6 link-local addresses are for use on a single link, for purpose ...

  6. SNMP协议交互学习-获取udp的udpindatagrams

    MIB的组织结构,如下左图,对于udp来说1.3.6.1.2.1.7,组织如下右图,包括4个标量和1个表格 udp节点在LwIP中的定义如下: ] = { , , , , }; ] = { (stru ...

  7. mongoDB常用命令与安全加固

    一.介绍 MongoDB 是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  8. vue-cli3点滴

    1.如果你不在构造函数中声明private的变量,那么久会提示错误. 2.

  9. 数组翻转(非reverse)

    var arr = [1,2,3,4]; var arr2 = []; while(arr.length) { var num = arr.pop(); //删除数组最后一个元素并返回被删除的元素 a ...

  10. ASP .NET CORE 源码地址

    ASP .NET CORE 源码地址:https://github.com/dotnet/ 下拉可以查找相应的源码信息, 例如:查找 ASP .NET CORE Microsoft.Extension ...