简体   繁体   中英

Load a file using JavaScript

I'm trying to load myself a file into my .js file, but I can't seem to find any code that explains it well enough. At the moment I have some code that will draw a flat triangle using webGL. Now I want to load up a model format (3DS, LAY, VW, OBJ etc etc), but I can't find any code to just load up a simple file and store itself inside a variable, now this javaScript file is used on a html file of course, but this html file won't be uploaded for random people to use, so there is no need to speak about the whole security file loading, all I want to try and do is load a local file into a variable, the file name will also be specified in the javaScript file anyways.

Just some simple text from a OBJ file

...
v  -0.3393 0.0000 -12.3639
v  1.8409 3.7515 -8.3253
v  2.8119 0.0000 -11.9490
v  -0.3393 3.7515 -8.6123
v  0.8700 -0.0000 -4.7016
v  -0.3393 -0.0000 -4.8608
...
vn -0.0000 0.0000 -1.0000
vn 0.0152 0.9983 -0.0567
...
vt 0.0000 0.0000 0.0000
vt 0.2500 0.0417 0.0000
...
f 1/1/1 2/2/2 3/3/3 
f 1/1/1 4/4/4 2/2/2 

Ajax, sir, is the way javascript "loads" text based files, on a wepbage.

I'd recommend a simple library to help you, as cross browser compatibility can be tricky.

Like say this one which uses the jQuery API for Ajax.

ajax({
  url: "/models/my_snazzy_model.obj",
  success: function(data) {
    console.log(data); // data is the text of your file. Woot!
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM