Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX上传PDF_Javascript_Php_Jquery_Ajax_File Upload - Fatal编程技术网

Javascript 使用AJAX上传PDF

Javascript 使用AJAX上传PDF,javascript,php,jquery,ajax,file-upload,Javascript,Php,Jquery,Ajax,File Upload,我正在尝试使用AJAX上传PDF文档,但一直失败,出现未知错误。我做错了什么 HTML文件: <form id="document"> <p> Title<br> <input type="text" name="name" size="30"> </p> <p> Please specify a file, or a set of files:<

我正在尝试使用AJAX上传PDF文档,但一直失败,出现未知错误。我做错了什么

HTML文件:

<form id="document">
    <p>
        Title<br>
        <input type="text" name="name" size="30">
    </p>
    <p>
        Please specify a file, or a set of files:<br>
        <input type="file" name="datafile" size="40">
    </p>
    <div>
        <input id="submit-button" type="button" value="Send">
    </div>
</form>

<script src="jquery.js"></script>
<script>
    $(document).ready(function(){
        $('#submit-button').click(function() {
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: "upload_document.php",
                data:  $("#document").serialize(),
                success : function(data){
                    alert(data.message);
                }, error : function(data) {
                    alert(data.message);
                }
            });
        });
    });
</script>


标题

请指定一个文件或一组文件:

$(文档).ready(函数(){ $(“#提交按钮”)。单击(函数(){ $.ajax({ 类型:“POST”, 数据类型:“JSON”, url:“upload_document.php”, 数据:$(“#文档”).serialize(), 成功:功能(数据){ 警报(数据、消息); },错误:函数(数据){ 警报(数据、消息); } }); }); });
PHP文件(upload\u document.PHP)


当您开始使用AJAX POST时,应该在$\u POST而不是$\u文件中查找POST参数

这是因为$\u文件是通过多部分post上传的文件的缓存。由于已经使用AJAX序列化并发送了JSON,PHP解析JSON并将所有内容放入$\u POST中


查找一个示例

我看到的一个问题是,您从生成的名称而不是从正在上载的文件中获取$fileType,它应该是这样的
$fileType=pathinfo($\u FILES[“datafile”][“tmp\u name”],pathinfo\u扩展名)移动上传的文件时失败。所以我确信它得到了文件,因为它通过了PDF文件扩展名检查
<?php

header("Access-Control-Allow-Origin: *");

try {
    $id = "[RANDOM_GENERATED_GUID]";

    $targetDir = "../../../../modules/sites/documents/";
    if (!is_dir($targetDir)) {
        if (!mkdir($targetDir, 0777, true)) {
            throw new Exception("Unable to upload your document. We were unable to create the required directories");
        }
    }

    $targetFile = $targetDir . $id . ".pdf";
    $fileType = pathinfo($targetFile, PATHINFO_EXTENSION);

    if (file_exists($targetFile)) {
        throw new Exception("Unable to upload your document. The file already exists");
    }

    if ($_FILES["datafile"]["size"] > 2000000) {
        throw new Exception("Unable to upload your document. The file is to large (Maximum of 2MB)");
    }

    if ($fileType != "pdf") {
        throw new Exception("Unable to upload your document. Only PDF documents can be uploaded");
    }

    if (!move_uploaded_file($_FILES["datafile"]["tmp_name"], $targetFile)) {
        //Keeps failing here with error code 0
        throw new Exception("Unable to upload your document. There was an error uploading the file");
    }

    echo json_encode(array(
        "error" => false,
        "message" => "Your document was successfully uploaded"
    ));
} catch (Exception $ex) {
    echo json_encode(array(
        "error" => true,
        "message" => $ex->getMessage()
    ));
}