将blob从javascript发送到php并将其保存为服务器上的文件

我使用Three.js导入了一些Geometry,并希望将有效的二进制.stl文件从它导出到我们的服务器。 Filesaver(Filesaver.js)创建的blob生成了一个有效文件,可以在下载时预览。 到目前为止... </ p>

为了将其保存到服务器,我创建了一个formData对象,并将文件名,dataURL和我的blob附加到它。 要将其发送到服务器,单击按钮时将执行此JS函数。</ p>

  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);
}
</ code> </ pre>

我的php文件如下所示:</ p>

 &lt;?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);

}
?&gt;
</ code> </ pre>

当我这样做时,我的文件夹中没有文件:/我已经能够在文件写入文件了 只传递一个字符串,因此应该为目标文件夹正确设置所有权限。 当我尝试使用JSON.stringify()解码JS中的二进制数据并传递它时,它工作正常,但保存的文件不再有效。</ p>

那么这里有什么问题? 我正在寻找解决方案几天并尝试了几件事,但没有进一步......:/ </ p>

我在这里缺少什么? 任何人都可以帮助我吗? </ p>

THX
Michael </ p>
</ div>

展开原文

原文

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

1个回答



$ content is undefined
而且使用file_put_contents而不是使用
https://secure.php.net/manual/en/function.move-uploaded-file.php </ a > </ p>
</ div>

展开原文

原文

$content is undefined Also instead of using file_put_contents you can just use https://secure.php.net/manual/en/function.move-uploaded-file.php

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐