質問

I'm using Dropzone to upload a file to 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();
}

It seems to run fine but it spits out the error "Server responded with 0 code." So I guess that means the server didn't respond. I have verified that the url is correct. on the server I have a component called com_hr4conduit. I have a controller called haf, and it has a method called 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 is a private function that spits out a JSON response with _arDebug and _arErr; preventing Joomla from spitting out the template.

To test this whole thing i created a simple file on my desktop called 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 is a site on my VM where Joomla is installed. This form works just fine. Is this a CORS issue? I am loading the page at https://x.hr4.mdev/ and am posting to https://hr4.mdev/. I am sending back the appropriate header:

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();

I think I'm feeling a little angry!

役に立ちましたか?

解決

OK, so it was indeed CORS. Since my sub-domain is on the same server, I uploaded the file to the same domain (x.hr4.mdev) using Dropzone. I got a response back from the server that contained the location of the file within the server file system. I was able to use that to achieve the desired result on hr4.mdev.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top