Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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
Php Dropzone上传至Joomla_Php_Joomla_Joomla3.0_Dropzone.js - Fatal编程技术网

Php Dropzone上传至Joomla

Php Dropzone上传至Joomla,php,joomla,joomla3.0,dropzone.js,Php,Joomla,Joomla3.0,Dropzone.js,我正在使用Dropzone将文件上载到Joomla: // ...bunch of JS this.buildUploader = function() { var objThis = this; if (typeof Dropzone != "undefined") { Dropzone.autoDiscover = false; this.dropZone = new Dropzone(this.fileSelect, {

我正在使用Dropzone将文件上载到Joomla:

// ...bunch of JS
this.buildUploader = function() {
    var objThis = this;
    if (typeof Dropzone != "undefined") {
        Dropzone.autoDiscover = false;
        this.dropZone = new Dropzone(this.fileSelect, {
            url : location.protocol + '//' + document.domain,
            autoProcessQueue : false,
            success : this.fileUploaded.bind(objThis),
            error: function(f, err) { console.log(err); },
            addRemoveLinks : true,
            sending: function(file, xhr, formData) {
                formData.append("option", "com_hr4conduit");
                formData.append("controller", "haf");
                formData.append("task", "saveFile");
                formData.append("s", document.getElementById('sid').value);
            }
        });
    }
}

this.doFileUpload = function() {
    console.log('do upload');
    this.dropZone.processQueue();
}
它似乎运行正常,但它抛出了一个错误“服务器以0代码响应”。所以我猜这意味着服务器没有响应。我已验证url是否正确。在服务器上,我有一个名为com_hr4conduit的组件。我有一个名为haf的控制器,它有一个名为saveFile的方法:

public function saveFile() {
  $storeFolder = 'images' . DS . 'attachments';
  $dataOut = new stdClass;
  $this->_arDebug[] = json_encode($_FILES);

  if (!empty($_FILES)) {
    $filename = $_FILES['file']['name'];
    $tempFile = $_FILES['file']['tmp_name'];
    $targetPath = JPATH_ROOT . DS . $storeFolder . DS;
    $targetFile =  $targetPath . $filename;
    move_uploaded_file($tempFile, $targetFile);
    $dataOut->fname = $filename;
  } else {
    $this->_arErr[] = "No files.";
  }
  $this->_dataOut($dataOut);
}
$this->_dataOut是一个私有函数,它用_arDebug和_arErr发出JSON响应;防止Joomla吐出模板

为了测试整个过程,我在桌面上创建了一个名为delme.html的简单文件:

<html>
<body>
<form method="post" action="https://hr4.mdev/" enctype="multipart/form-data" >
  <input type="hidden" name="s" value="*an authentication key*" />
  <input type="hidden" name="option" value="com_hr4conduit" />
  <input type="hidden" name="controller" value="haf" />
  <input type="hidden" name="task" value="saveFile" />
  <input type="file" name="file" />
  <input type="submit" value="submit" />
</form>
</body>
</html>

hr4.mdev是我的虚拟机上安装Joomla的一个站点。这个表格很好用。这是CORS的问题吗?我正在加载页面并将其发布到。我正在发回相应的标题:

JResponse::clearHeaders();
JResponse::setHeader( 'Expires', 'Mon, 1 Jan 2001 00:00:00 GMT', true );
JResponse::setHeader( 'Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT', true );
JResponse::setHeader( 'Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false );
JResponse::setHeader( 'Access-Control-Allow-Origin', '*', false); // <- SEE!
JResponse::sendHeaders();
JResponse::clearHeaders();
JResponse::setHeader('Expires','Mon,2001年1月1日00:00:00 GMT',true);
JResponse::setHeader('Last Modified',gmdate(“D,dmy H:i:s”),'GMT',true);
JResponse::setHeader('Cache Control','no store,no Cache,必须重新验证,post check=0,pre check=0',false);

JResponse::setHeader('Access Control Allow Origin','*',false);// 好的,那确实是CORS。因为我的子域在同一台服务器上,所以我使用Dropzone将文件上载到同一个域(x.hr4.mdev)。我从包含文件在服务器文件系统中的位置的服务器得到了一个响应。我能够使用它在hr4.mdev上实现所需的结果。

浏览器使用
HEAD
HTTP请求查看文档是否支持CORS。HTTP服务器不会响应任何数据,因此不会看到您的
JResponse::setHeader
调用(文档根本不会被解析)。使用.htaccess或等效方法将这些头添加到响应头中。