后台:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; /**
*
* @Description :文件上传
* @version : 1.0
* @Date : 2016年4月28日 下午1:17:53
*/
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController {
private static final Logger log = LoggerFactory.getLogger(UploadController.class);
@RequestMapping("/index")
public String index() {
return "upload/upload";
} /**
* 上传
*
* @param files
* @param request
* @param response
* @return
*/
@RequestMapping("/files")
public @ResponseBody String file(@RequestParam MultipartFile[] files, HttpServletRequest request,
HttpServletResponse response, String path) {
String url = "";
try {
//
for (MultipartFile file : files) {
// 此处编写业务代码处理,组合url }
System.out.println("上传文件路径:" + url);
return url.substring(0, url.length() - 1);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
}
return url;
}
}

页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>上传文件</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"
src="<%=path%>/resources/js/jquery/jquery-1.8.3.min.js"></script>
<script type="text/javascript"> $(document).ready(function(){
$("#upload").click(function() {
//FormData支持文件ajax上传
//使用 jQuery 来发送 FormData,但必须要正确的设置相关选项:
//processData: false, // 告诉jQuery不要去处理发送的数据
//contentType: false // 告诉jQuery不要去设置Content-Type请求头
//支持浏览器:Chrome 7+, Firefox(2.0) 4.0, Internet Explorer 10+, Opera 12+,Safari 5+
var formData = new FormData($("#uploadForm")[0]);
$.ajax({
url : "<%=path%>/upload/files.html",
data : formData,
type : "post",
dataType : "text",
timeout : 3600000,
async : true,
cache : false,
contentType : false,
processData : false,
success : function(data) {
if (data == "") {
alert("上传文件失败!");
} else {
$("#url").text(data);
}
},
error : function(data) {
alert(data);
}
});
}); });
</script> </head> <body>
<center>
<form id="uploadForm" enctype="multipart/form-data" method="post">
选择文件:<input type="file" name="files" multiple="multiple"><br />
<br /> <input type="button" value="上传" id="upload">
</form>
上传成功文件路径:<label id="url" />
</center>
</body>
</html>

FormData对象实现文件Ajax上传的更多相关文章

  1. models渲染字典&form表单上传文件&ajax上传文件

    {# {% for u in teacher_d.keys %}#} {# {% for u in teacher_d.values %}#} {% for k,u in teacher_d.item ...

  2. fromdata上传文件,ajax上传文件, 纯js上传文件,html5文件异步上传

    前端代码: 上传附件(如支付凭证等) <input type="file" name="fileUpload" id="fileUpload&q ...

  3. 利用html5的FormData对象实现多图上传

    <html> <head> <title>FormData多图上传演示</title> </head> <body> <a ...

  4. 异步上传文件,ajax上传文件,jQuery插件之ajaxFileUpload

    http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 一.ajaxFileUpload是一个异步上传文件的jQuery插件. ...

  5. 玩转图片上传————原生js XMLHttpRequest 结合FormData对象实现的图片上传

    var form=document.getElementById("formId"); var formData=new FormData(form); var oReq = ne ...

  6. Django框架 之 Form表单和Ajax上传文件

    Django框架 之 Form表单和Ajax上传文件 浏览目录 Form表单上传文件 Ajax上传文件 伪造Ajax上传文件 Form表单上传文件 html 1 2 3 4 5 6 7 <h3& ...

  7. Jquery FormData文件异步上传 快速指南

    网站中文件的异步上传是个比较麻烦的问题,不过现在通过jquery 可以很容易的解决这个问题: 使用jquery2.1版本,较老版本不支持异步文件上传功能: 表单代码: <form id=&quo ...

  8. django系列6--Ajax05 请求头ContentType, 使用Ajax上传文件

    一.请求头ContentType ContentType指的是请求体的编码类型,常见的类型共有三种: 1.application/x-www-form-urlencoded 这应该是最常见的 POST ...

  9. 在jquery中,使用ajax上传文件和文本

    function onSubmit (data) { //获取文本 var callingContent = $('#callingContent').val() // 获取文件 var files ...

随机推荐

  1. php文件和目录操作函数

    文件:打开和关闭:fopen(), fclose()读:readfile(), file(), file_get_contents(), fgets(), fgetss(), fgetc()写:fwr ...

  2. Linux和Windows路由配置

    Linux和Windows路由配置 一.配置路由 1-       原则上一台主机只能有一条缺省路由.如果一台主机上有多个网段的话,请配置能够上网的那个网段的网关为缺省路由 Linux配置缺省路由: ...

  3. rpyc 回调模式工作不正常

    rpyc 回调模式工作不正常 最近使用了 rpyc 来处理一个多节点间的文件同步的任务,目标是使用 rpyc 来实现简单的 p2p 文件传输机制,以减少单点负载和单点失败对传输的影响. 和 p2p 的 ...

  4. 第二篇 Replication:分发服务器的作用

    本篇文章是SQL Server Replication系列的第二篇,详细内容请参考原文. 分发服务器是SQL Server复制的核心组件.分发服务器控制并执行数据从一个服务器移动到另一个服务器的进程. ...

  5. html 二级联动(省市联动)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. nginx地址重写

    1. 域名重定向 server_name wx.he.com weixin.ha.com; if ($http_host !~* "wx\.he\.com"){ rewrite ^ ...

  7. Oracle 监听器日志文件过大导致监听异常

    Oracle 监听器日志文件过大导致监听异常 db版本:11.2.0.1 os版本:windows2008 现象: 应用异常,无法连接数据库.登陆数据库服务器,查看监听已经断掉.尝试重启监听,重启失败 ...

  8. iOS-self.layer.shouldRasterize属性

    当shouldRasterize设成true时,layer被渲染成一个bitmap,并缓存起来,等下次使用时不会再重新去渲染了.实现圆角本身就是在做颜色混合(blending),如果每次页面出来时都b ...

  9. docker note

    docker --bip="10.1.42.1/16" -d 挂载宿主机目录 Docker支持挂载宿主机目录,支持宿主机目录和容器之间文件目录进行映射,彼此共享: docker r ...

  10. Android项目目录结构分析

    Android项目目录结构分析 1.HelloWorld项目的目录结构1.1.src文件夹1.2.gen文件夹1.3.Android 2.1文件夹1.4.assets 1.5.res文件夹1.6.An ...