Flask jQuery Ajax Upload File

Source: Internet
Author: User

1. HTML code
 <Div>        <formID= "Uploadform"enctype= "Multipart/form-data" >            <Div>                <inputtype= "File"name= "File"value=""style= "Color:white">                <inputtype= "button"value= "Upload"ID= "Upfilebtn">            </Div>        </form>  </Div>    

Note: 1, HTML is mainly a form form, where the form of enctype = "Multipart/form-data" must have.

2, because my page background color set to black, so the font color selected white, this harmless.

varUp_file =function () {varFormData =NewFormData ($ (' #uploadForm ') [0]);$.ajax ({URL:"/test/up_file", type:"POST", Data:formdata, async:true, Cashe:false, ContentType:false, ProcessData:false, Success:function(returndata) {Alert (Returndata)
},
Error: function (returndata) {
    Alert ("Upload failed! ")

})

Note: If you want to upload a file with Ajax, you will use Formdata to convert the file to a Formdata object.

3, Flask part
@test. Route ('/up_file', methods=['GET','POST'])defup_file ():ifRequest.method = ="POST": File= request.files['file']       #file_name = "Test.csv"file_name =file.filename File.save (Os.path.join ('Templates\\files', file_name)) return 'Upload Successful'

Note: 1, this article does not write the blueprint part, I believe that the configuration of the route is not difficult.

2, this code will exist in the Server Templates files folder, you can directly to the file to take a new name, you can also use rerquest.files[' file '].filename to get the name of the upload file.

Flask jQuery Ajax Upload File

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.