ajax 文件上传,ajax

啥也不说了,直接上代码!

<input type="file" id="file" name="myfile" onclick="clearProgressInfo()"/>
<span id="progressInfo" style="display:none;">
<progress id="progressBar" value="0" max="100"></progress>
<span id="percentage"></span>
</span><br/>
<input type="button" onclick="UpladFile()" value="上传" />

js代码

function UpladFile() {
var fileObj = document.getElementById("file").files[0]; // js 获取文件对象
var FileController = "/file/saveFile.do"; // 接收上传文件的后台地址 // FormData 对象
var form = new FormData();
form.append("author", "hooyes"); // 可以增加表单数据
form.append("file", fileObj); // 文件对象 // XMLHttpRequest 对象
var xhr = new XMLHttpRequest();
xhr.open("post", FileController, true);
xhr.onload = function() {
// alert("上传完成!");
}; document.getElementById('progressInfo').style.display = "";
xhr.upload.addEventListener("progress", progressFunction, false); xhr.send(form);
} function clearProgressInfo()
{
document.getElementById('progressInfo').style.display='none';
document.getElementById("progressBar").value = 0;
document.getElementById("percentage").innerHTML = "";
} 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) + "%";
}
}

java后台处理:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.util.Streams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import com.okcoin.util.Logs; @Controller
@RequestMapping(value="/file/*.do")
public class FileController extends MultiActionController{ public String saveFile(HttpServletRequest request,HttpServletResponse response) throws IOException
{
MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
MultipartHttpServletRequest multipartRequest = null;
try {
multipartRequest = resolver.resolveMultipart(request);
} catch (Exception e) {
Logs.geterrorLogger().error("AdminAccountController submitVerifyFile 非常的请求");
} //接收文件
MultipartFile documentFile = multipartRequest.getFile("file");
//获取文件名
String documentFileName=documentFile.getOriginalFilename();
System.out.println("FileName:" + documentFileName); BufferedInputStream in = new BufferedInputStream(documentFile.getInputStream());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\Users\\" + documentFileName));
Streams.copy(in, out, true);
System.out.println("copy finished.");
return "";
}
}

ajax 文件上传,ajax的更多相关文章

  1. python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)

    昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...

  2. Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件

    一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...

  3. AJAX文件上传实践与分析,带HTML5文件上传API。

    对于HTML5已经支持AJAX文件上传了,但如果需要兼容的话还是得用一点小技巧的,HTML5等等介绍,先来看看以前我们是怎么写的. 网上可能会有一些叫AJAX文件上传插件,但在AJAX2.0之前是不可 ...

  4. 兼容ie的jquery ajax文件上传

    Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...

  5. jQuery插件AjaxFileUpload实现ajax文件上传

    转自:http://www.cnblogs.com/linjiqin/p/3530848.html jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个 ...

  6. 转: 如何实现jQuery的Ajax文件上传

    [PHP文件上传] 在开始之前,我觉得是有必要把通WEB上传文件的原理简单说一下的.实际上,在这里不管是PHP,JSP,还是ASP处理上传的文件,其实都是WEB早已把文件上传到服务器了,我们只是运用上 ...

  7. [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传

    原文 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件 ...

  8. php+ajax文件上传

    php+ajax文件上传 html: <input id="user_real_name" class="input_show" type="t ...

  9. springmvc+ajax文件上传

    环境:JDK6以上,这里我是用JDK8,mysql57,maven项目 框架环境:spring+springmvc+mybaits或spring+springmvc+mybatis plus 前端代码 ...

随机推荐

  1. zoj 3757 Alice and Bob and Cue Sports 月赛A 模拟

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3757 题意:根据所给的台球规则,按照每次的结果计算最终两人的得分 ...

  2. CentOS系统cobbler完全部署及案例测试

    本篇内容注主要涉及cobbler架构快速搭建,Cobbler命令行语法简单应用,Cobbler-WEB,system-config-kickStart基于Windows界面配置生成ks脚本模板,ks简 ...

  3. Ehcache简单说明及使用

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存 ...

  4. winform 分页 分类: WinForm 2014-05-16 15:30 257人阅读 评论(0) 收藏

    说明:(1)如果对分页的感兴趣的话,可以看一下我传的存储过程("SQL 存储过程 分页")               (2)分页,第一页.上一页,下一页.最后一页只调用点击(cl ...

  5. PullToRefreshListView调用onRefreshComplete方法 无法取消刷新的bug

    我们在使用框架:   PullToRefreshListView 实现下拉或者上拉加载时候,可能在上拉 完成时候,调用onRefreshComplete方法去 停止 刷新操作,但是,可能无效,测试产生 ...

  6. PPT 学习总结

    PPT高效制作流程1.把word导入PPT2.在PPT里实现随机抽奖3.逻辑的调整:给PPT增加小节,来增强PPT的逻辑性4.设计PPT的母板,并可保存为自己的母板供以后使用 数据收集中国宏观经济统计 ...

  7. Android核心基础(二)

    1.对应用进行单元测试 在实际开发中,开发android软件的过程需要不断地进行测试.而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测 ...

  8. [React] React Fundamentals: Integrating Components with D3 and AngularJS

    Since React is only interested in the V (view) of MVC, it plays well with other toolkits and framewo ...

  9. etrace 跟踪程序函数动态执行流程

    https://github.com/elcritch/etrace 窗口1: 监控窗口,执行监控程序,显示监控结果 [root@monitor example]# pwd /root/etrace- ...

  10. centos安装g++

    安装: yum -y install gcc automake autoconf libtool make 安装g++: yum install gcc gcc-c++