weixin_33713350 2017-10-25 10:35 采纳率: 0%
浏览 19

Ajax发布有以前的数据

This is driving me crazy. I know I am doing something wrong but what? The problem is I am getting previous data in my ajax call when I post it to php.

Story: I click upload button which calls JavaScript loadme() where i am cleaning the input type file element. I then select file/files and click upload which calls doit() which internally calls uploadFiles(). UploadFiles makes an array with loaded images and post it to the php file through ajax.

I have tried plain javascript and jquery to clean file input element and php to set post values to null after using it, but still i am getting the previous values.

The form

<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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误
    • ¥15 用visual studi code完成html页面
    • ¥15 聚类分析或者python进行数据分析
    • ¥15 三菱伺服电机按启动按钮有使能但不动作
    • ¥15 js,页面2返回页面1时定位进入的设备