Jump to content

BABYLON.SceneLoader.ImportMesh without URL


desMaker
 Share

Recommended Posts

Hello, 

i want to import a File with the BABYLON.SceneLoader.ImportMesh, but i don't have a URL, because the file comes form a Form. 

 

i have input form:



<html>
...
<input type="file" id="upload_file" name="upload" />
...
</html>


 

and i try to import my Mesh (.babylon-File) out of the form, without uploading it.

But i don't know how to import a file without a given URL. 

Please Help me :-)

 



<script>...


var inputElement = document.getElementById("upload_file");
inputElement.addEventListener("change", function handleFiles(event) {


var fileList = this.files[0];


// The first parameter can be used to specify which mesh to import. Here we import all meshes
BABYLON.SceneLoader.ImportMesh("", "", "data:" + fileList, scene, function (newMeshes) {
// Set the target of the camera to the first imported mesh
camera.target = newMeshes[0];
});


}, false);


...

</scrip>

 


 

 

 

Thanks!

Link to comment
Share on other sites

Hi,

 

Look into the File API, Blob and FileReader (https://developer.mozilla.org/en-US/docs/Web/API/FileReader) , might come in handy. After you load the file and you have its content, you can use Babylon's import function to load the scene.

 

You can see how I used a blob url to load a scene in the material loader - https://github.com/RaananW/BabylonJS-Material-Editor/blob/master/MaterialEditor/canvas/CanvasService.ts#L48

Link to comment
Share on other sites

Thanks, it was exactly what I was looking for! Here is my code:

                        var inputElement = document.getElementById("input");                        inputElement.addEventListener("change", function handleFiles(event) {                            var fileList = this.files[0];                            var reader = new FileReader();                            reader.addEventListener("loadend", function () {                                var data = reader.result;                                // The first parameter can be used to specify which mesh to import. Here we import all meshes                                BABYLON.SceneLoader.ImportMesh("", "", "data:" + data, scene, function (newMeshes) {                                });                            });                            reader.readAsText(fileList);                      }, false);
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...