我確實有以下頁面中的一個正在做拖放和另一個獲取文件寫入。如果拖放在本地進行,則在遠程情況下不會出現問題。php和javascript不能發佈文件到遠程服務器,而發佈到本地服務器的工作
<!DOCTYPE html>
<html>
<head>
<title> BETA APP HOME PAGE </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<link rel="stylesheet" href="CSS/style.css">
</head>
<body>
<ul>
<li><a class="active" href='demo_upload.php'>demo upload</a></li>
</ul>
<div style="margin-left:15%;padding:1px 16px;height:1000px;">
<a id="topright" href="#" title="RaspBerry Pi"></a>
<h1> DEMO USING FILE UPLOAD</h1>
<p></p>
<?php
if (isset($_POST['STARTSCRIPT']))
{
$command = escapeshellcmd('sudo python AppPy/cgi-bin/test.py');
$output = shell_exec($command);
echo("on");
echo $output;
}
if (isset($_POST['STOPSCRIPT']))
{
shell_exec("sudo python AppPy/cgi-bin/test.py");
echo("Off");
}
?>
<form method="post">
<button name="STARTSCRIPT">START SCRIPT</button>
<button name="STOPSCRIPT">STOP SCRIPT</button><br><br>
</form>
<div id="dragandrophandler">Drag & Drop Files Here </div>
<br><br>
<div id="status1"></div>
<script>
function sendFileToServer(formData,status)
{
var uploadURL ="http://192.168.56.153/WEBAPP/upload.php"; //Upload URL
var extraData ={}; //Extra Data.
var jqXHR=$.ajax({
xhr: function() {
var xhrobj = $.ajaxSettings.xhr();
if (xhrobj.upload) {
xhrobj.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position/total * 100);
}
//Set progress
status.setProgress(percent);
}, false);
}
return xhrobj;
},
url: uploadURL,
type: "POST",
contentType:false,
processData: false,
cache: false,
data: formData,
success: function(data){
status.setProgress(100);
//$("#status1").append("File upload Done<br>");
}
});
status.setAbort(jqXHR);
}
var rowCount=0;
function createStatusbar(obj)
{
rowCount++;
var row="odd";
if(rowCount %2 ==0) row ="even";
this.statusbar = $("<div class='statusbar "+row+"'></div>");
this.filename = $("<div class='filename'></div>").appendTo(this.statusbar);
this.size = $("<div class='filesize'></div>").appendTo(this.statusbar);
this.progressBar = $("<div class='progressBar'><div></div></div>").appendTo(this.statusbar);
this.abort = $("<div class='abort'>Abort</div>").appendTo(this.statusbar);
obj.after(this.statusbar);
this.setFileNameSize = function(name,size)
{
var sizeStr="";
var sizeKB = size/1024;
if(parseInt(sizeKB) > 1024)
{
var sizeMB = sizeKB/1024;
sizeStr = sizeMB.toFixed(2)+" MB";
}
else
{
sizeStr = sizeKB.toFixed(2)+" KB";
}
this.filename.html(name);
this.size.html(sizeStr);
}
this.setProgress = function(progress)
{
var progressBarWidth =progress*this.progressBar.width()/ 100;
this.progressBar.find('div').animate({ width: progressBarWidth }, 10).html(progress + "% ");
if(parseInt(progress) >= 100)
{
this.abort.hide();
}
}
this.setAbort = function(jqxhr)
{
var sb = this.statusbar;
this.abort.click(function()
{
jqxhr.abort();
sb.hide();
});
}
}
function handleFileUpload(files,obj)
{
for (var i = 0; i < files.length; i++)
{
var fd = new FormData();
fd.append('file', files[i]);
var status = new createStatusbar(obj); //Using this we can set progress.
status.setFileNameSize(files[i].name,files[i].size);
sendFileToServer(fd,status);
}
}
$(document).ready(function()
{
var obj = $("#dragandrophandler");
obj.on('dragenter', function (e)
{
e.stopPropagation();
e.preventDefault();
$(this).css('border', '2px solid #0B85A1');
});
obj.on('dragover', function (e)
{
e.stopPropagation();
e.preventDefault();
});
obj.on('drop', function (e)
{
$(this).css('border', '2px dotted #0B85A1');
e.preventDefault();
var files = e.originalEvent.dataTransfer.files;
//We need to send dropped files to Server
handleFileUpload(files,obj);
});
$(document).on('dragenter', function (e)
{
e.stopPropagation();
e.preventDefault();
});
$(document).on('dragover', function (e)
{
e.stopPropagation();
e.preventDefault();
obj.css('border', '2px dotted #0B85A1');
});
$(document).on('drop', function (e)
{
e.stopPropagation();
e.preventDefault();
});
});
</script>
</div>
</div>
<div style="margin-left:15%;padding:1px 16px;height:10px;">
</div>
</body>
</html>
我的問題是,當我說明我目前的服務器:
var uploadURL ="http://192.168.56.153/WEBAPP/upload.php"
它的工作,這一點:
chmod -R 0777 /var/www/html/WEBAPP/
這僅僅是爲了測試(獲得的權利問題車程) ,我從apache訪問日誌中得到本地服務器的響應:
192.168.56.1 - - [08/Apr/2017:14:07:44 +0200] "POST /WEBAPP/upload.php HTTP/1.1" 200 203 "http://192.168.56.153/WEBAPP/demo_upload.php" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
我的文件正在上傳。
但是,當我改變了這個目的地遠程:
var uploadURL ="http://192.168.56.154/WEBAPP/upload.php"
我在遠程服務器Apache訪問日誌此消息:
192.168.56.1 - - [08/Apr/2017:13:44:13 +0200] "OPTIONS /WEBAPP/upload.php HTTP/1.1" 200 203 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
沒有文件上傳。
的upload.php的文件是相同的兩種情況:
<?php
$ds = DIRECTORY_SEPARATOR; //1
$storeFolder = 'uploads'; //2
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; //3
$targetPath = dirname(__FILE__) . $ds. $storeFolder . $ds; //4
$targetFile = $targetPath. $_FILES['file']['name']; //5
move_uploaded_file($tempFile,$targetFile); //6
}
?>
這是[CORS「預檢請求」](https://www.w3.org/TR/cors/#resource-preflight-requests)[相關問題](http://stackoverflow.com/questions/ 1256593 /爲什麼我得到一個選項請求,而不是一個請求) – segFault
好吧...所以我必須添加這樣的內容:contentType:「text/plain」?但如果我想要照片? – MouIdri