【问题标题】:Importing File using PHP Spreadsheet使用 PHP 电子表格导入文件
【发布时间】:2020-03-03 09:21:44
【问题描述】:

我正在尝试使用 ajax 导入 Excel 工作表,但是当我尝试上传时它总是返回 null。我正在使用 PHP 电子表格。

这是我的 HTML 代码:

    <div class="modal-body">
                    <?php echo form_open_multipart('', array('id' => 'form_upload_user')); ?>
                    <input type="hidden" name="csrf_lendingtree_app" value="<?php echo $this->security->get_csrf_hash(); ?>" />
                    <h3 class="upload_header_title">UPLOAD FILE</h3>
                    <div class="input-file-upload">
                        <input type="file" name="upload_file" multiple>
                        <img class="upload_icon" src="<?php echo base_url('metronic/theme/classic/assets/images/upload-icon.png'); ?>" alt="">
                    </div>
                    <a href="<?php echo base_url('leaderboard/User/template'); ?>" classs="upload_download_template">Download template</a>
                    <div class="upload_btn_action">
                        <button type="button" class="btn-user-back" name="button" data-dismiss="modal">BACK</button>
                        <button type="submit" class="btn-user-ok" name="button">OK</button>
                    </div>
                </form>
            </div>

这是我的 ajax 代码:

    $('#form_upload_user').on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: base_url + "User/bulk_upload_users",
            data: new FormData(this),
            dataType: "json",
            contentType: false,
            cache: false,
            processData: false,
            success: function(data) {
                //if success close modal and reload ajax table
                $("input[name='csrf_app']").each(function() {
                    $(this).val(data['csrf_app']);
                });
                console.log(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert('Error adding / update data');
            }
        });
    });

这是我的 php 代码:

    public function bulk_upload_users()
    {
        $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        if(isset($_FILES['upload_file']['name']) && in_array($_FILES['upload_file']['type'], $file_mimes)) {
            $arr_file = explode('.', $_FILES['upload_file']['name']);
            $extension = end($arr_file);
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
            $spreadsheet = $reader->load($_FILES['upload_file']['tmp_name']);
            $sheetData = $spreadsheet->getActiveSheet()->toArray();
            echo json_encode(array("status" => $sheetData,"csrf_app" => $this->security->get_csrf_hash()));
        }else{
            echo json_encode(array("status" => "Not Exist","csrf_app" => $this->security->get_csrf_hash()));
        }
   }

我总是收到File "" does not exist. 的错误,有人遇到过这种错误吗?

任何帮助将不胜感激。

我已经被卡住了,我不知道如何解决这个错误。

【问题讨论】:

    标签: php codeigniter phpspreadsheet


    【解决方案1】:

    您应该将条件从 isset($_FILES['upload_file']['name']) 更改为 $_FILES['upload_file']['name']!=''

    所以在你的 php 代码中试试这个:

    public function bulk_upload_users()
    {
        $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        if($_FILES['upload_file']['name']!= "" && in_array($_FILES['upload_file']['type'], $file_mimes)) {
            $arr_file = explode('.', $_FILES['upload_file']['name']);
            $extension = end($arr_file);
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
            $spreadsheet = $reader->load($_FILES['upload_file']['tmp_name']);
            $sheetData = $spreadsheet->getActiveSheet()->toArray();
            echo json_encode(array("status" => $sheetData,"csrf_app" => $this->security->get_csrf_hash()));
        }else{
            echo json_encode(array("status" => "Not Exist","csrf_app" => $this->security->get_csrf_hash()));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多