简体   繁体   中英

use jquery to upload file?

How can you use jquery to simply post to a php script the value of a file? I'm assuming the Files array and post array are different, so would I have to use a different method? ex. would posting var file = $('fileInput').val(); work? What data am I supposed to send and how should I send it? Thanks

Use this to post files using jquery (html5 browsers supporting FileAPI only, others like IE don't support it):

        var PostData = new FormData();

        $('form').find(':file').each(function () {
            var inputName = $(this).attr('name');
            for(var i = 0; i < this.files.length; i++){
                PostData.append(inputName + '_' + i, this.files[i]);
            }
        });

        var Request = $.ajax({
            url: 'uploadPage.php',
            cache: false,
            contentType: false,
            dataType: 'json',
            type: 'post',
            data: PostData,
            processData : false,
            success: function (data){
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM