2016-11-14 55 views
0

我使用Dropzone JS和從我的前端(localhost:9000),我打電話upload.php我得到的圖像,然後將其上傳到後端(localhost:80 )在一個文件夾中。 這裏是我的html:Dropzone JS無法上傳文件

<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 ([email protected]_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); 
    } 
} 

如果我上傳的圖片,在控制檯(對於每一個瀏覽器) it

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

我想要做的是在本地主機加載圖片:80/progettoTimelinegit/API /上傳/

+0

我從HTTPS使用教程:// WWW .startutorial.com /篇/圖/如何到內建一個文件上傳 - 成型 - 使用 - dropzonejs及PHP – Fedeco

回答

0

upload.php -file已經在api -folder,只是改變這一行:

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

到:

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

它應該工作。你有很多奇怪的變量,未使用的變量等,所以也可能有其他的錯誤。

0

我設法使它工作,非常貴4天的工作,但現在它的工作原理: 你必須在你的代碼的某些部分小心:

的Html

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

「多部分/格式數據」 使懸浮窗AUTOMATICALLY組OPTIONS請求

虛擬主機或一個的.ini /的.htaccess與XAMPP/wampp /遊民

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表單懸浮窗,如果你有加密類型,你得寫 在你的虛擬主機或HTA重寫規則ccess
  2. 你寫了重寫規則爲了使cors Successfull?
  3. 確保你的路徑有所有權限和上傳文件夾存在

  4. 檢查靜態路徑,無論你正在做一個HTTP請求