Ajax帖子有以前的数据

时间:2017-10-25 10:35:53

标签: javascript php jquery ajax

这让我发疯了。我知道我做错了什么但是什么?问题是当我将它发布到php时,我在我的ajax调用中获取了以前的数据。

故事:我点击上传按钮,调用JavaScript loadme(),我正在清理输入类型文件元素。然后我选择文件/文件,然后单击上传调用doit(),内部调用uploadFiles()。 UploadFiles创建一个带有加载图像的数组,并通过ajax将其发布到php文件中。

我已经尝试过普通的javascript和jquery来清理文件输入元素和php,在使用后将post值设置为null,但我仍然得到以前的值。

表格

<form id="theform" method="post" action="" enctype="multipart/form-data" target="multifile">
   <input type="hidden" id="folderpath" name="folderpath" value="<?=htmlspecialchars($path)?>">
   <input name="upfiles[]" id="upfiles" type="file" multiple="" />
</form>

Javascript

async function doit(){
  //some code
  uploadFiles();
}

function loadmeup(){
  $('upfiles').value = '';    
  $('uploadbox').show();
}

function closeupload(){
  $('uploadbox').hide();
}

var masterFileArray = [];
var left_loaded_count = 0;
var progress = 0;

function uploadFiles() {
  var files = document.getElementById("upfiles").files;
  var path = document.getElementById('folderpath').value;
  var numOfFiles = files.length;
  alert(numOfFiles);

  if (files && numOfFiles>0) {
    for (var i = 0, f; f = files[i]; i++) {
        if(checkExtension(f.name) && checkSize(f.size)){
            progress = i+1/numOfFiles;
            var r = new FileReader();
            r.onload = (function (f) {
                return function (e) {
                    var contents = e.target.result;
                    masterFileArray.push({name:f.name, contents: contents, type:f.type, size:f.size}); 
                    console.log(f.name);
                    left_loaded_count -= 1;
                    if(left_loaded_count == 0)
                    {
                        var filesarray = null;
                        filesarray = JSON.stringify(masterFileArray);
                        console.log("after stringify: "+filesarray.length);
                        new Ajax.Request('upload_do.php', {
                            method: 'post',
                            parameters: {files: filesarray, folderpath:path},
                            onSuccess: function(transport){
                                var response = transport.responseText;
                                parent.notify(response);
                                closeupload();
                                get_listview(currlist,sort);                                    
                            }
                        });
                    }
                };
            })(f);
            left_loaded_count += 1;
            r.readAsDataURL(f);
        }
        else{
            alert('Invalid file extension or size ' + f.name);
            if(progress==0)
                location.reload();
        }
    }
  } else {
    alert('Failed to load files');
    closeupload();
  }
}

PHP

try{
  $error = null;
  $files = json_decode($_POST['files'],true);
  $numFiles = 0;
  foreach($files as $file){
    $output = $reso->writeUpload($file['contents'], $file['name']);
    $numFiles++;
  }
}
catch (Exception $ex){
  $error = $ex->getMessage();
  $GLOBALS['log']->log($error);
}
isset($_POST['files'])? $_POST['files'] = null: '';
if (!is_null($error))
  echo _($error);
else
  echo _($numFiles.' file'.(($numFiles > 1) ? 's' : '').' successfully 
uploaded');

0 个答案:

没有答案