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

    报告相同问题?

    悬赏问题

    • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
    • ¥15 关于arduino编程toCharArray()函数的使用
    • ¥100 vc++混合CEF采用CLR方式编译报错
    • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
    • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
    • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
    • ¥50 如何openEuler 22.03上安装配置drbd
    • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
    • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
    • ¥15 Windows11, backspace, enter, space键失灵