Home > Web Front-end > JS Tutorial > body text

Using the FormData object and Spring MVC to implement the Ajax file download function (graphic tutorial)

亚连
Release: 2018-05-22 09:49:24
Original
1549 people have browsed it

This article mainly introduces the use of FormData objects and Spring MVC to implement the Ajax file download function. Friends in need can refer to

Ajax file download

Using the FormData object and Spring MVC to implement the Ajax file upload function:

Steps

1. Import components and prepare Static script


<dependency>
 <groupId>commons-fileupload</groupId>
 <artifactId>commons-fileupload</artifactId>
 <version>1.3.2</version>
</dependency>
  <h1>Ajax 文件上载</h1>
  <input type="file" id="file1"> <br>
  <input type="file" id="file2"> <br>
  <input type="button" id="upload" value="上载" >
  <p id="result"></p>
Copy after login


1. Bind events to buttons


$("upload").click(ajaxUpload);
Copy after login


2. Get the file


var file1 = $("#file1")[0].files[0];
var file2 = $("#file2")[0].files[0];
Copy after login


3. Create a form object in memory and add to Data transmitted by the server


//创建内存中的表单对象
var form = new FormData();
//向其中添加要传输的数据
form.append("userfile1", file1);
form.append("userfile2", file2);
Copy after login


4.ajax() upload object


$.ajax({
  url:&#39;user/upload.do&#39;,//请求地址
  data: form,   //请求参数
  type: &#39;POST&#39;,  //请求类型
  dataType: &#39;json&#39;,//服务器返回的数据类型
  contentType: false,//没有设置任何内容类型头信息
  processData: false, //见jQuery_api详解
  success: function(obj){ //成功时回调函数,obj表示服务器返回的数据
    if(obj.state==0){
      $(&#39;#result&#39;).html("成功!"); 
    }
  }
});
Copy after login


5.Spring-MVC presentation layer


@RequestMapping("/upload.do")
@ResponseBody
public JsonResult upload( 
    MultipartFile userfile1, 
    MultipartFile userfile2) throws Exception{
  //Spring MVC 中可以利用 MultipartFile 
  //接收 上载的文件! 文件中的一切数据
  //都可以从 MultipartFile 对象中找到
  //获取上再是原始文件名
  String file1 = 
    userfile1.getOriginalFilename();
  String file2 = 
    userfile2.getOriginalFilename();
  System.out.println(file1);
  System.out.println(file2);
  //保存文件的3种方法:
  //1. transferTo(目标文件)
  //  将文件直接保存到目标文件, 可以处理大文件
  //2. userfile1.getBytes() 获取文件的全部数据
  //  将文件全部读取到内存, 适合处理小文件!!
  //3. userfile1.getInputStream()
  //  获取上载文件的流, 适合处理大文件
  //保存的目标文件夹: /home/soft01/demo
  File dir = new File("D:/demo");
  dir.mkdir();
  File f1 = new File(dir, file1);
  File f2 = new File(dir, file2);
  //第一种保存文件
  //userfile1.transferTo(f1);
  //userfile2.transferTo(f2);
  //第三种 利用流复制数据
  InputStream in1 = userfile1.getInputStream();
  FileOutputStream out1 = 
    new FileOutputStream(f1);
  int b;
  while((b=in1.read())!=-1){
    out1.write(b);
  }
  in1.close();
  out1.close();
  InputStream in2 = userfile2.getInputStream();
  FileOutputStream out2=
      new FileOutputStream(f2);
  byte[] buf= new byte[8*1024];
  int n;
  while((n=in2.read(buf))!=-1){
    out2.write(buf, 0, n);
  }
  in2.close();
  out2.close();
  return new JsonResult(true);
  }
Copy after login

The above is what I compiled for everyone, I hope it will be useful to everyone in the future helpful.

Related articles:

Based on apicloudAJAX request code collection (absolutely detailed)

Native JS implements Ajax cross-domain request flask Response content (graphic tutorial)

Ajax detection method for website hijacking

The above is the detailed content of Using the FormData object and Spring MVC to implement the Ajax file download function (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!