预览 上传图片 视频 预览
html:
<video src="" id="video0" controls="controls"></video> <input type="file" id="video" name="video"/>
js代码:
// hTML5实现表单内的上传文件框,上传前预览视频,刷新预览video,使用HTML5 的File API, // 建立一个可存取到该file的url,一个空的video标签,ID为video0,把选择的文件显示在video标签中,实现视频预览功能。 // 需要选择支持HTML API的浏览器。 $("#video").change(function(){ var objUrl = getObjectURL(this.files[0]) ; if (objUrl) { $("#video0").attr("src", objUrl) ; //img.src=objUrl ,图片预览也类似 } }) ; //建立一个可存取到该file的url, createObjectURL兼容处理 function getObjectURL(file) { var url = null ; if (window.createObjectURL!=undefined) { // basic url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file) ; } else if (window.webkitURL!=undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file) ; } return url ; //输出格式 blob:http://localhost:8082/dd56dfec-12ef-4532-ae88-f1a2e6802f2c }
上传 blob格式的内容
var formData = new FormData(); formData.append("type","2"); //提交type=2的值给后台 formData.append("file",blob,"recorder.mp3"); //提交blob数据给后台 // console.log(formData.values()); for (var value of formData.values()) { console.log(value); } $.ajax({ url:"xxx" //上传接口地址 ,type:"POST" ,contentType:false //让xhr自动处理Content-Type header,multipart/form-data需要生成随机的boundary ,processData:false //不要处理data,让xhr自动处理 ,data:formData ,success:function(v){ console.log("上传成功",v); } ,error:function(s){ console.error("上传失败",s); } });