Javascript reading files: how should I have asked?

In summary: I meant what format does the json data come back in.In summary, you used a server-side program to create the necessary javascript containing your data that could then be retrieved by the browser and then processed on your web page.
  • #1
Trying2Learn
373
57
TL;DR Summary
IO in javascript: what is the question
Hello

A few weeks ago (March 7) I asked how to have Javascript read a data file of numbers into an array.

I now understand why this is such a challenge when running a code on a web page, locally.

I have since found a sufficient workaround. I hope I can share it in the hope that someone can tell me how I SHOULD have asked the question. (Yes, this is an odd question: I am actually asking FOR a question.)

Here is what I did.

After Matlab processed all the numbers, I had Matlab open a file named "Rotation.js"
Matlab wrote the lines into Rotation.js to create an open of a function... then it deposited the data array.
Then it wrote the lines to "close the function."
Then it closed the file.

Then I ran the *.html file that ran the thereJS code. The main.js called the function that Matlab created, and displayed the
results in 3D.

So... I know that is a silly thing to have done (I KNOW I could have coded in OpenGL, or written the javascript code to process
the data. But considering the complexity of the Runge-Kutta method on six differential equations, this was a good workaround.

What SHOULD I have asked to have gotten this result? Again, i am not asking for an answer or even a better way. I just want to know how I could have asked the question.

Here is the section from Matlab to show you what I did.fileID = fopen('get_RotationMatrix.js','w');
fprintf(fileID,'function get_RotationMatrix(){\n');
fprintf(fileID,'var rotation = [\n');
for i = 1:timeIntegrationSteps
fprintf(fileID,'\"%12.8f\",\n',omega1(i));
end
fprintf(fileID,'];\n');
fprintf(fileID,'return rotation;\n}\n');
fclose(fileID);
 
Technology news on Phys.org
  • #2
Nice workaround. In essence, you used a server-side program to create the necessary javascript containing your data that could then be retrieved by the browser and then processed on your web page.

In the context of javascript, your question is without meaning. I suppose if you had asked about processing data in Matlab and then how to display it on a web page you might gotten better responses but we'll never know as programming is such a diverse platform of recipes to do the same thing in different ways.

The problem with your question is that:
1) you are using javascript that traditionally works in a sandbox on a browser where local files simply can't be read or written.
2) you need to do things outside the browser to get it to work

NodeJS is a framework/server where you can use javascript on both the server-side and browser-side. In this case, javascript running on NodeJS can access files to read and to write.

You might read your binary data, compute stuff and then write out a JSON file with the data. There are methods that write data in JSON format for you.

The browser side of your application could request a computation and then retrieve the JSON file to process and display.
 
  • Like
Likes Trying2Learn and FactChecker
  • #3
Trying2Learn said:
What SHOULD I have asked to have gotten this result?
How about "I want to load a data file into an HTML page (so I can use it as input to the 3D renderer Three.js), how can I do this? I want all this to be local, not through a web server or anything."
 
  • Like
Likes Trying2Learn
  • #4
jedishrfu said:
The browser side of your application could request a computation and then retrieve the JSON file to process and display.
That won't work with a local file, the XMLHttpRequest api won't work with file:// and although a script tag will happily parse a JSON file there is no way to access the data in the user sandbox.
 
Last edited:
  • #5
My apologies, that’s not what I meant the json data is sent back to the browser via the response to the request.
 

What is the purpose of reading files in Javascript?

Reading files in Javascript allows us to access and manipulate data stored in external files, such as text files or JSON files. This can be useful for data analysis, data processing, or building dynamic web applications.

How can I read a file in Javascript?

There are several methods for reading files in Javascript, including the FileReader API, Node.js file system module, and AJAX. The method you choose will depend on your specific needs and environment.

What types of files can be read with Javascript?

Javascript can read a variety of file types, including text files, JSON files, and XML files. However, it is important to note that Javascript cannot read files that are stored on a server or outside of the browser's sandbox environment for security reasons.

What is the difference between synchronous and asynchronous file reading in Javascript?

Synchronous file reading means that the program will wait for the file to be completely read before moving on to the next line of code. Asynchronous file reading, on the other hand, allows the program to continue running while the file is being read. Asynchronous reading is typically used for larger files to improve performance.

Are there any limitations to reading files in Javascript?

Yes, there are some limitations to reading files in Javascript. For example, Javascript cannot read files that are outside of the browser's sandbox environment or files that are stored on a server. Additionally, some browsers may have security restrictions that prevent certain file reading methods from working.

Similar threads

  • Programming and Computer Science
Replies
6
Views
978
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
7K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
2
Views
317
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
Back
Top