繁体   English   中英

Dropzone JS无法上传文件

[英]Dropzone JS not able to upload file

我正在使用Dropzone JS,并从前端(localhost:9000)调用了upload.php,在其中获取图像,然后将其上传到文件夹中的后端(localhost:80)。 这是我的html:

 <form action="localhost:80/ProgettoTimelinegit/api/upload.php" class="dropzone" id="my-dropzone" enctype="multipart/form-data">

 </form>

我的javascript:

Dropzone.autoDiscover = false;
$("#my-dropzone").options = { 
    maxFilesize: 4, // MB
    url:"http://localhost:80/ProgettoTimelinegit/api/upload.php",
    addRemoveLinks : true,
    uploadMultiple:true,
    paramName:"file",
    parallelUploads: 2,
    maxFiles: 10,
    autoProcessQueue: true,
    headers: {
        // remove Cache-Control and X-Requested-With
        // to be sent along with the request
        'Cache-Control': null,
        'X-Requested-With': null
    }
};

和upload.php

$ds = DIRECTORY_SEPARATOR;  //1
$storeFolder = '/xampp/htdocs/images/';   //2

if(!empty($_FILES)) {
    // START // CREATE DESTINATION FOLDER
    define('DESTINATION_FOLDER','../api/upload/');

    if (!@file_exists(DESTINATION_FOLDER)) {
        if (!mkdir(DESTINATION_FOLDER, 0777, true)) {
            $errors[] = "Destination folder does not exist or no permissions to see it.";
        }

        // END // CREATE DESTINATION FOLDER
        $temp = $_FILES['file[]']['tmp_name'];
        $dir_seperator = "fold/";

        //$destination_path = dirname(__FILE__).$dir_seperator.$folder.$dir_seperator;
        $destination_path = DESTINATION_FOLDER.$dir_seperator;
        $target_path = $destination_path.(rand(10000, 99999)."_".$_FILES['file']['name']); 
        move_uploaded_file($temp, $target_path);
    }
}

如果我上传图片,请在控制台(对于每个浏览器)中

POST localhost:80/ProgettoTimelinegit/api/upload.php net::ERR_UNKNOWN_URL_SCHEME

我想做的是将图像加载到localhost:80 / progettoTimelinegit / api / upload /

upload.php -file已经在api -folder,只是改变这一行:

define('DESTINATION_FOLDER','../api/upload/');

至:

define('DESTINATION_FOLDER','upload/');

它应该工作。 您有很多奇怪的变量,未使用的变量等,因此也可能存在其他错误。

我设法使它起作用,花了我4天的时间,但现在它起作用了:在代码的某些部分必须要小心:

HTML

Do not add enctype="multipart/form-data" or if you do remember to add rewrite rule

“ multipart / form-data”使dropzone 自动设置选项REQUEST

虚拟主机或带有Xampp / wampp / vagrant的.ini / .htAccess

VirtualHost *:80>
    DocumentRoot "#folderOfyourWebsite"
    ServerName yoursite.name
     <Directory "#path To The HTML of Your Index Page">
         Options Indexes FollowSymLinks MultiViews ExecCGI
         AllowOverride Authconfig FileInfo
         Require all granted
        </Directory>
        Header set Access-Control-Allow-Origin "*"      
        Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
        Header set Access-Control-Max-Age "1000"
        Header set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]
  </VirtualHost>

在重写引擎中,我们告诉他:好的,如果您收到一个选项请求,请用200重写它(成功的帖子)

因此,您的待办事项清单可以重新列出:

  1. 检查您的html dropzone表单,如果您具有enctype,则必须在vhost或htaccess中编写一个重写规则
  2. 您是否编写了重写规则以使cors成功?
  3. 确保您的路径具有所有权限,并且您的上传文件夹存在

  4. 无论您执行的是http请求,都请检查您的静态路径

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM