FormData对象实现文件Ajax上传
后台:
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上传的更多相关文章
- models渲染字典&form表单上传文件&ajax上传文件
{# {% for u in teacher_d.keys %}#} {# {% for u in teacher_d.values %}#} {% for k,u in teacher_d.item ...
- fromdata上传文件,ajax上传文件, 纯js上传文件,html5文件异步上传
前端代码: 上传附件(如支付凭证等) <input type="file" name="fileUpload" id="fileUpload&q ...
- 利用html5的FormData对象实现多图上传
<html> <head> <title>FormData多图上传演示</title> </head> <body> <a ...
- 异步上传文件,ajax上传文件,jQuery插件之ajaxFileUpload
http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 一.ajaxFileUpload是一个异步上传文件的jQuery插件. ...
- 玩转图片上传————原生js XMLHttpRequest 结合FormData对象实现的图片上传
var form=document.getElementById("formId"); var formData=new FormData(form); var oReq = ne ...
- Django框架 之 Form表单和Ajax上传文件
Django框架 之 Form表单和Ajax上传文件 浏览目录 Form表单上传文件 Ajax上传文件 伪造Ajax上传文件 Form表单上传文件 html 1 2 3 4 5 6 7 <h3& ...
- Jquery FormData文件异步上传 快速指南
网站中文件的异步上传是个比较麻烦的问题,不过现在通过jquery 可以很容易的解决这个问题: 使用jquery2.1版本,较老版本不支持异步文件上传功能: 表单代码: <form id=&quo ...
- django系列6--Ajax05 请求头ContentType, 使用Ajax上传文件
一.请求头ContentType ContentType指的是请求体的编码类型,常见的类型共有三种: 1.application/x-www-form-urlencoded 这应该是最常见的 POST ...
- 在jquery中,使用ajax上传文件和文本
function onSubmit (data) { //获取文本 var callingContent = $('#callingContent').val() // 获取文件 var files ...
随机推荐
- C# Main函数的 args参数
网上参考 博客,使用如下代码: using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- Java基础——数组应用之字符串String类
字符串String的使用 Java字符串就是Unicode字符序列,例如串“Java”就是4个Unicode字符J,a,v,a组成的. Java中没有内置的字符串类型,而是在标准Java类库中提供了一 ...
- SQL Server翻译目录
从SQLServerCentral翻译部分Stairways文章,设置目录方便阅读(2015-12更新)SQL Server代理系列第一篇 SQL Server代理概述第二篇 SQL Server代理 ...
- python之map、filter、reduce、lambda函数
map map函数根据提供的函数对指定的序列做映射,定义:map(function, sequence[,sequence,...])--->list 例1 >>> map(l ...
- 一个想了好几天的问题——关于8086cpu自己编写9号中断不能单步的问题
在<汇编语言>第十五章中我们可能遇到这样的问题:程序运行正确,但是debug单步调试,却无法运行,修改int 9h中断例程入口地址的指令,虚拟模式下,debug提示指令无效, ...
- ads 错误
这个问题已经不是第一次碰到了,每次弄周立功的EasyARM2210的时候都会遇见,每次都没有记住.就是要用ADS运行板子配套光盘里面的配套程序的时候会出现: (Fatal)L6002U:Could n ...
- Lintcode: Median
Given a unsorted array with integers, find the median of it. A median is the middle number of the ar ...
- 算法提高 c++_ch02_01
http://lx.lanqiao.org/problem.page?gpid=T237 算法提高 c++_ch02_01 时间限制:1.0s 内存限制:512.0MB 编写一个程 ...
- HDU 4834 JZP Set(数论+递推)(2014年百度之星程序设计大赛 - 初赛(第二轮))
Problem Description 一个{1, ..., n}的子集S被称为JZP集,当且仅当对于任意S中的两个数x,y,若(x+y)/2为整数,那么(x+y)/2也属于S.例如,n=3,S={1 ...
- MySQL 中NULL和空值的区别 (转载 http://blog.sina.com.cn/s/blog_3f2a82610102v4dn.html)
平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...