Dropzone JS无法上传文件

时间:2016-11-14 10:15:47

标签: javascript php image-uploading dropzone.js

我正在使用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 /

2 个答案:

答案 0 :(得分:0)

您的upload.php - 文件已在api - 文件夹中,只需更改此行:

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

为:

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

它应该有效。你有很多奇怪的变量,未使用的变量等,所以也可能有其他错误。

答案 1 :(得分:0)

我设法让它发挥作用,它花了我4天的工作但现在它的工作原理: 你必须小心你的代码的某些部分:

<强> HTML

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

&#34;多部分/格式数据&#34; make dropzone 自动设置选项请求

虚拟主机或带有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请求