FormData and Spring MVC implement Ajax file download function
This time I will bring you FormData and Spring MVC to implement the Ajax file download function. What are the precautions for FormData and Spring MVC to implement the Ajax file download function. The following is a practical case. , let’s take a look.
Ajax file download
Using the FormData object and Spring MVC to implement the Ajax file upload function:Steps
1. Import components and prepare static scripts<dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3.2</version> </dependency> <h1 id="Ajax-文件上载">Ajax 文件上载</h1> <input> <br> <input> <br> <input> <p></p>1. Bind events to buttons
$("upload").click(ajaxUpload);2. Get files
var file1 = $("#file1")[0].files[0]; var file2 = $("#file2")[0].files[0];3. Create the form object in memory and add the data transmitted to the server
//创建内存中的表单对象 var form = new FormData(); //向其中添加要传输的数据 form.append("userfile1", file1); form.append("userfile2", file2);4.ajax() upload object
$.ajax({ url:'user/upload.do',//请求地址 data: form, //请求参数 type: 'POST', //请求类型 dataType: 'json',//服务器返回的数据类型 contentType: false,//没有设置任何内容类型头信息 processData: false, //见jQuery_api详解 success: function(obj){ //成功时回调函数,obj表示服务器返回的数据 if(obj.state==0){ $('#result').html("成功!"); } } });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); }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:
How to implement WebApi Ajax cross-domain requests using CORS
How to implement dynamic loading of combo boxes with Ajax (With code)
The above is the detailed content of FormData and Spring MVC implement Ajax file download function. For more information, please follow other related articles on the PHP Chinese website!

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor
