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

将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

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3