简体   繁体   English

使用JavaScript加载文件

[英]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. 我正在尝试将自己的文件加载到我的.js文件中,但我似乎无法找到任何足以解释它的代码。 At the moment I have some code that will draw a flat triangle using webGL. 目前我有一些代码将使用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. 现在我想加载一个模型格式(3DS,LAY,VW,OBJ等),但我找不到任何代码只是加载一个简单的文件并将其自身存储在一个变量中,现在这个javaScript文件用于一个html文件,当然,但这个html文件不会上传给随机的人使用,所以没有必要谈论整个安全文件加载,我想尝试做的就是将一个本地文件加载到一个变量,无论如何,文件名也将在javaScript文件中指定。

Just some simple text from a OBJ file 只是OBJ文件中的一些简单文本

...
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. Ajax,先生,是javascript“加载”基于文本的文件的方式,在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. 就像说这个使用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!
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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