Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery的Ajax上传进度条_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 使用jQuery的Ajax上传进度条

Javascript 使用jQuery的Ajax上传进度条,javascript,jquery,ajax,Javascript,Jquery,Ajax,所以我并不是什么都用jQuery,但我确实用它来处理诸如AJAX之类的好东西,而现在这个“枪之子”给了我一些问题。下面是一段代码,在上传过程中应该更改我的上传进度条 function uploadProgess(){ var info = document.getElementById('infor'); var images = document.getElementsByName('file[]')[0].files; $.ajax({ before

所以我并不是什么都用jQuery,但我确实用它来处理诸如AJAX之类的好东西,而现在这个“枪之子”给了我一些问题。下面是一段代码,在上传过程中应该更改我的上传进度条

function uploadProgess(){
    var info = document.getElementById('infor');
    var images = document.getElementsByName('file[]')[0].files; 
    $.ajax({
        beforeSend: function(XMLHttpRequest){
            XMLHttpRequest.upload.addEventListener("progress", function(evt){
                if (evt.lengthComputable) {
                    document.getElementById('progress-bar').style.display="block";
                    var percentComplete = (evt.loaded / evt.total * 100) + '%';
                //Do something with upload progress
                    document.getElementById('progress').style.width = percentComplete;
                }
            }, false); 
        },
        type: 'POST',
        url: "administration.php?mode=styling&part=pics_management",
        data:{method:"upload",file:images},
        success: function(response) {
            info.innerHTML = response;
        },
        error:function(response){
            info.innerHTML = response;
        }
        }).done(function(){
        window.location.href = window.location.href;
    });
}
单击表单的submit按钮时会调用此函数

$('#upload_images').click(function(e){
 e.preventDefault();
 uploadProgess();
 }); 

但是我得到了
未捕获的类型错误:非法调用jQuery.js:4
不确定我调用了另一个属性做了什么,或者我做了什么“非法”的事情,有人能解释我代码中的错误吗?

这对我来说非常有效。。。注释在其他用户的代码中

function uploadProgess(){
    //cache our "info" popup element
    var info = document.getElementById('infor');
    //cache our input element
    var images = document.getElementsByName('file[]')[0];   
    //create a new FormData object (HTML5)
    var fileData = new FormData();
    //append type is upload
    fileData.append("type","upload");
    //append method is ajax (only allow ajax uploads)
    fileData.append("method","ajax");
    //get the files from the input element
    var files = images.files;
    //loop through the files
    for(i=0;i<files.length;i++){
        //append to the fileData with name of `file[]` and then the files object for each image
        fileData.append('file[]',files[i]);
    }
    //new ajax request
    var request = new XMLHttpRequest();

    //event listener progress
    request.upload.addEventListener('progress',function(event){
        if(event.lengthComputable){
            //cache the progress bar
            var progress = document.getElementById('progress-bar');
            //get our percentage
            var percent = (Math.round(event.loaded / event.total) * 100) +"%";
            //make the progress bar visible
            progress.style.display="block";
            //recache for the spinning bar inside progress
            progress = document.getElementById('progress');
            //change it's width to the percentage of upload
            progress.style.width = percent;
        }
    });
    //add event for when the progress is done
    request.upload.addEventListener('load',function(event){
        //cache progress bar
        var progress = document.getElementById('progress-bar');
        //hide the progress bar
        progress.style.display="none";
    });
     //for errors we'll use the info element but for now console log it
    request.upload.addEventListener('error',function(event){
        console.log(event);
    });
    //open the request
    request.open("POST","{URL}");
    //set the request header for no caching
    request.setRequestHeader("Cache-Control","no-cache");
    //send the data
    request.send(fileData);
}
函数uploadproges(){
//缓存我们的“信息”弹出元素
var info=document.getElementById('infor');
//缓存我们的输入元素
var images=document.getElementsByName('file[])[0];
//创建新的FormData对象(HTML5)
var fileData=new FormData();
//追加类型为上传
追加(“类型”、“上载”);
//append方法是ajax(仅允许ajax上传)
append(“方法”、“ajax”);
//从输入元素获取文件
var files=images.files;
//循环浏览文件

对于(i=0;我以前遇到过这个问题,当我的数据包含非JSON时。我指的是你的
文件:images
可能包含元素。是的,它确实包含了HAHA.wow。类似于这个bug的含义。让我看看是否可以对其进行排序。如果文件[]当然是一个数组,输入是多个文件,那么我将如何格式化文件[].files,但这不起作用。您使用的是哪个JQuery版本?我相信是1.11.1谢谢,我讨厌JQuery,因为我停止使用它了。