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