ajax file upload

后端 未结 3 1682
一整个雨季
一整个雨季 2021-01-07 00:35

I am struggling to get a file uploaded, processed and displayed without reloading the page. What do I use for jquery to get the file posted to the server properly?



        
3条回答
  •  萌比男神i
    2021-01-07 00:57

    You can upload a form containing INPUT[type="file"] without reloading the page by using jquery.upload.js. In the $.Deferred.done handler you can add your own code to display the server response.

    $.upload( form.action, new FormData( myForm))
    .progress( function( progressEvent, upload) {
        if( progressEvent.lengthComputable) {
            var percent = Math.round( progressEvent.loaded * 100 / progressEvent.total) + '%';
            if( upload) {
                console.log( percent + ' uploaded');
            } else {
                console.log( percent + ' downloaded');
            }
        }
    })
    .done( function( data) {
        console.log( 'Finished upload');
    
        // add your code for rendering the server response
        // into html here
    });
    

    You can get it here : https://github.com/lgersman/jquery.orangevolt-ampere/blob/master/public/lib/ampere/jquery.upload.js

提交回复
热议问题