【问题标题】:How to upload a pdf file from input with jquery to php?如何使用jquery将pdf文件从输入上传到php?
【发布时间】:2019-11-19 23:57:19
【问题描述】:

我正在使用向导在我的系统上完成用户配置文件。在第 7 步中,我使用向导加载 pdf 文档。我希望从输入中选择文档时自动加载,但我遇到了以下问题。

值得一提的是,我想用ajax加载它(这样过程是自动的)并发送到php保存在服务器上。

向导是这样的(作为参考): https://jsfiddle.net/yeyene/59e5e1ya/

我的意见:

<div class="box">
    <input type="file" name="file-7" id="file-7" class="inputfile inputfile-6" data-multiple-caption="{count} files selected" multiple />
        <label for="file-7">
            <span></span> 
            <strong>
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/>
                </svg> 
                Find pdf &hellip;
            </strong>
        </label>
</div>

这是我的 js

$('#file-7').change(function(e){
    var file_data = this.files[0];
    //var file_data = $(this).prop('files')[0];
    console.log(file_data);
        if(file_data != undefined) {
            var datos = new FormData();
            datos.append('sendpdf', true);                  
            datos.append('pdf', file_data);
            $.ajax({
                url:"views/ajax.php",
                method:"POST",
                data:datos,
                contentType:false,
                processData:false,
                beforeSend: function() {
                    $("#loading").removeClass('hidden');
                },
                success: function(regreso){
                    $("#loading").addClass('hidden');

                    console.log(regreso);
                }
            });
        }  
});

这是我的 ajax.php

if (isset($_POST["sendpdf"])) {
    var_dump($_POST['pdf']);
}

错误:

console.log(file_data);

谢谢你的帮助。

【问题讨论】:

  • console.log(file_data); 显示什么?
  • @KalimahApps 好的,我已经更新了帖子。
  • 我不确定确切的问题。可以尝试对文件对象进行编码吗?使用这一行 datos.append('pdf', btoa(file_data)); 并在您的 PHP 文件中添加 base64_decode ( $_POST['pdf'])
  • @KalimahApps 太好了,我已经收到了 php 文件,但我认为它到达时是空的 prntscr.com/pzdg7a
  • 它不是空的。它是一个对象。尝试使用json_decode(base64_decode ( $_POST['pdf']))

标签: php jquery arrays


【解决方案1】:

只有我需要在 php 中接收这些代码:

$_FILES['file']

【讨论】:

    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2014-04-08
    • 2021-05-14
    • 1970-01-01
    • 2019-03-29
    相关资源
    最近更新 更多