Use the FormData object and Spring MVC to implement the Ajax file download function.

Source: Internet
Author: User

Use the FormData object and Spring MVC to implement the Ajax file download function.

Ajax File Download

The combination of FormData object and Spring MVC enables Ajax file upload:

Procedure

1. Import components and prepare static scripts

<Dependency> <groupId> commons-fileupload </groupId> <artifactId> commons-fileupload </artifactId> <version> 1.3.2 </version> </dependency> 

1. Bind event to button

$("upload").click(ajaxUpload);

2. Get the file

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

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

// Create the form object var form = new FormData () in the memory; // Add the data form to be transmitted to it. append ("userfile1", file1); form. append ("userfile2", file2 );

4. Upload objects using ajax ()

$. Ajax ({url: 'user/upload. do ', // request address data: form, // request parameter type: 'post', // request type dataType: 'json', // the data type returned by the server contentType: false, // NO content type header information is set processData: false, // For details, see jQuery_api success: function (obj) {// callback function upon success, obj indicates the data returned by the server if (obj. state = 0) {success ('{result'}.html ("successful! ");}}});

5. Spring-MVC presentation layer

@ RequestMapping ("/upload. do") @ ResponseBodypublic JsonResult upload (MultipartFile userfile1, MultipartFile userfile2) throws Exception {// MultipartFile // can be used in Spring MVC to receive uploaded files! All data in the file // can be found from the MultipartFile object // get the original file name String file1 = userfile1.getOriginalFilename (); String file2 = userfile2.getOriginalFilename (); System. out. println (file1); System. out. println (file2); // three methods for saving files: // 1. transferTo (target file) // save the file directly to the target file, which can process large files. // 2. userfile1.getBytes () obtains all the data of a file // reads all the files to the memory. It is suitable for processing small files !! // 3. userfile1.getInputStream () // gets the stream of the uploaded File. It is suitable for processing large files. // The target folder to be saved:/home/soft01/demo File dir = new File ("D: /demo "); dir. mkdir (); File f1 = new File (dir, file1); File f2 = new File (dir, file2); // first save the File // userfile1.transferTo (f1 ); // userfile2.transferTo (f2); // The third method uses stream replication data 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 );}

Summary

The above section describes how to use the FormData object and Spring MVC to download Ajax files. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.