springmvc-implement put request upload file

Source: Internet
Author: User

Because the image is uploaded using binary mode, the method of using hidden fields is invalid, and the method of conversion is as follows:

Https://www.cnblogs.com/morethink/p/6378015.html

But then I had another requirement. That is, when the changes need to transfer files to the Put method, so this method is not OK, but I see in the HiddenHttpMethodFilter source code this sentence

1   Case of a multipart 2   for checking a POST body parameter.</b>3  * so typically, put a Spring {@link Org.springfra Mework.web.multipart.support.MultipartFilter}4This   hiddenhttpmethodfilter in Your {@code web. xml} filter chain.

and multipartfilter the source code in such comments

1  /** 2      * Set The bean name of the multipartresolver to fetch from Spring ' s3     * Root application con Text. Default is "Filtermultipartresolver". 4      */

  1. That means we can register a multipartfilter in Web. XML,be sure to Hiddenhttpmethodfilter before the
    1<filter>2<filter-name>MultipartFilter</filter-name>3<filter-class>org.springframework.web.multipart.support.multipartfilter</filter-class>4</filter>5<filter-mapping>6<filter-name>MultipartFilter</filter-name>7<servlet-name>dispatcher</servlet-name>8</filter-mapping>9 Ten<filter> One<filter-name>HiddenHttpMethodFilter</filter-name> A<filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class> -</filter>
  2. Then add the following code in the Spring root application context
    <bean id= "Filtermultipartresolver"      class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">    <property name=" Maxuploadsize " Value= "209715200"/>    <property name= "defaultencoding" value= "UTF-8"/>    <property name= " Resolvelazily "value=" true "/></bean>
  3. The Formdata object is an object of HTML5, and some of the mainstream browsers are now compatible. The Formdata object is an object of HTML5, and some of the mainstream browsers are now compatible.
    function Test () {var form=NewFormData (document.getElementById ("TF")); Form.append ("_method", ' Put '); $.ajax ({url:url, type:' Post ', Data:form, ProcessData:false, ContentType:false, Success:function (data) {Window.clearinterval (timer); Console.log ("Over.."); }, Error:function (e) {alert ("Error!! ");            Window.clearinterval (timer);        }        }); Get ();//here is the progress bar for uploading files    }<form id= "TF" method= "POST" name= "Formdada" enctype= "Multipart/form-data" > <input type= "file" name= "file"/&    Gt <input type= "text" name= "id"/> <input type= "text" name= "name"/> <input type= "button" value= "Lift" onclic k= "Test ()"/></form>

Finally, it is possible to submit a file upload to the Put method.

springmvc-implement put request upload file (go)

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.