doqp87012 2018-03-21 08:35
浏览 778

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

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条回答 默认 最新

  • doulei1965 2018-03-21 08:56
    关注

    $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

    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常