简体   繁体   English

使用jQuery上传文件?

[英]use jquery to upload file?

How can you use jquery to simply post to a php script the value of a file? 您如何使用jquery简单地将文件的值发布到php脚本? I'm assuming the Files array and post array are different, so would I have to use a different method? 我假设Files数组和post数组不同,所以我必须使用其他方法吗? ex. 例如 would posting var file = $('fileInput').val(); 将发布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): 使用它通过jquery发布文件(仅支持FileAPI的html5浏览器,IE等其他浏览器不支持):

        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){
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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