前台表单:

 <form id="uploadform" method="post" enctype="multipart/form-data">
上传视频:<input type="file" id="file" name="file" onchange="uploadfile()"/>
进度:
     <progress id="progressBar" value="0" max="100"></progress>
    <span id="percentage"></span>
</form>

js代码:

<script type="text/javascript">

function uploadfile() {
var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
var FileController = "teacher/uploadvideo.action"; // 接收上传文件的后台地址 // FormData 对象
var form = new FormData($( "#uploadform" )[0]); // XMLHttpRequest 对象
var xhr = new XMLHttpRequest();
xhr.open("post", FileController, true);
xhr.onload = function () {
// alert("上传完成!");
}; xhr.upload.addEventListener("progress", progressFunction, false);
xhr.send(form);
} function progressFunction(evt) {
var progressBar = document.getElementById("progressBar");
var percentageDiv = document.getElementById("percentage");
if (evt.lengthComputable) {
progressBar.max = evt.total;
progressBar.value = evt.loaded;
percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
if(evt.loaded==evt.total){
alert("上传完成100%");
}
}
}
</script>

后台(这个传的opid是教师编号,这些代码是我从自己的项目中提取出来的,但知识是一样的):

@RequestMapping("/teacher/uploadvideo")
public void uploadVideo(@RequestParam("file") MultipartFile uploadFile,String opid,HttpServletRequest request) throws IllegalStateException, IOException {
System.out.println(opid);
//获取文件初始名称
String originalFileName = uploadFile.getOriginalFilename(); Operation ope = operationService.getOpeByid(opid); String video = ope.getVideourl();
String houzhui = originalFileName.substring(originalFileName.lastIndexOf(".")); //上传文件
String newFileName = UUID.randomUUID()+houzhui;
File newFile = new File(videoUrl,newFileName);
uploadFile.transferTo(newFile); //删除原有文件
String oldVideoUrl = videoUrl + "/" + video;
File videoFile = new File(oldVideoUrl);
if(videoFile.exists()) {
videoFile.delete();
} ope.setVideourl(newFileName);
operationService.modify(ope);
}

最新文章

  1. unix下输出重定向
  2. Unity2D之让土豆人动起来
  3. php加密解密
  4. Webservce、WCF、WebApi的区别
  5. mongo 安装
  6. [原创]Devexpress XtraReports 系列 10 创建标签报表
  7. STL:remove和erase区别
  8. 第七篇: python高级之多线程
  9. CoreCLR文档翻译 - GC的设计
  10. Jmeter添加监控指标
  11. ecshop商品页增加编辑器fckeditor
  12. RADIUS and IPv6[frc-3162译文]
  13. 复杂DIV交错布局
  14. angular高级篇之transclude使用详解
  15. SpringBoot究竟是如何跑起来的?
  16. 首席技术官应该考虑的网络安全问题 IT大咖说 - 大咖干货,不再错过
  17. Linux在终端命令行模式下智能补全功能以及组合键
  18. JAVA 程序的基本语法
  19. hadoop fs 获取文件大小
  20. 小程序获取地址授权的修改 wx.openSetting需点击

热门文章

  1. 巧妙设置Texture Type,将ShadowMask内存占用变成之前的1/4
  2. JSP的执行原理
  3. Dubbo工作原理,集群容错,负载均衡
  4. S-CMS企建v3二次SQL注入
  5. [Swift]LeetCode136. 只出现一次的数字 | Single Number
  6. Visual Studio 2017 怎么将自动生成属性设置为旧版格式
  7. dataframe的select传入不定参数
  8. Xamarin.Android多窗口传值【1】
  9. 获取Ip所在城市名与详细
  10. nginx替换响应头(重点:如何在替换时加上if判断)