I imported some Geometry with Three.js and want to export a valid binary .stl file from it to our server. The blob created by the Filesaver (Filesaver.js) resulted in a valid file, which can be previewed when downloaded. So far...
For saving it to the server I create a formData object and append filename, dataURL and my blob to it. To send it to the server, This JS function is executed when a button is clicked.
function exportSTL(){
var export_stl = new THREE.STLBinaryExporter();
var sceneEx = new THREE.Scene();
var model = new THREE.Object3D();
model.add(frame.getObjectByName("base_surf").clone());
model.add(frame.getObjectByName("logo_front_surf").clone());
model.add(frame.getObjectByName("logo_back_surf").clone());
model.add(frame.getObjectByName("new_text_mesh").clone());
model.add(frame.getObjectByName("text_surf").clone());
sceneEx.add(model);
var output = export_stl.parse(sceneEx);
var blob = new Blob([output] , {type: "application/vnd.ms-pki.stl"});
//saveAs(blob, filename);
var objectURL = URL.createObjectURL(blob);
console.log(objectURL);
var filename = "-" + ".stl";
var data = new FormData();
data.append('name', filename);
data.append('tmp_name', objectURL);
data.append('data', blob);
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', 'index.php', true);
xhr.send(data);
}
My php file looks like this:
<?php
if(!empty($_POST['data'])){
$data = $_POST['data'];
$fname = $_POST['name'];
$fileBlob = $_FILES['data'];
$tmpPath = $fileBlob['tmp_name'];
file_put_contents('/www/htdocs/w0132394/konfig.project-samsen.com/stl/' + $fname , $content);
}
?>
When I do this, there is no file in my folder:/ I was already able to write text to files when I was passing only a string, so all rights should be set correctly for the destination folder. when I tried to decode the binary data in JS with JSON.stringify() and passed it,it worked, but the file saved was not valid anymore.
So what is the problem here? I am searching for a solution for a couple of days and tried several things, but didn't get any further... :/
What am I missing here? Can anybody help me with that?
THX Michael