I want to read a JSON file that in my local machinge using Javescript, and parse it to a Javascript object such as this code:
var parsed = JSON.parse(JSON_file);
Is it possible to read the file from the disk? Or there is other methods to do that.
Thanks.
Here is a nice tutorial on how you can do it using HTML5's FileReader API, but there is one constraint: you can only interact with a file selected by the user via a file input field.
http://www.html5rocks.com/en/tutorials/file/dndfiles/
Related
I'm using the excel-as-json module (https://github.com/stevetarver/excel-as-json) and I have set it up so that it translates my .xlsx files to .json, but I now changed it so that the .xlsx is uploaded by the user in the front-end of the app.
I would like to run the convert on the uploaded Excel file, but since I am getting the user to upload it - I don't actually have a path to it, only the file object itself. So excel-as-json tells me that it cannot find the src file [Object object]
The excel as json function call is:
convertExcel(src, dst, options, callback);
What is the best way to pass the object as src? What if I store the .xlsx in my mongoDB, could I pass in its path easier then?
Not solution, but workaround: Get user to copy in path to file and use this. Based on answer from: Getting file full path when uploading file in html in firefox
Note this workaround only works for localhost
Solution I solved my issue by using a different module - sheet.js. I get the user to upload their file using an <input> tag and then use sheet.js to parse it into json, before sending it to the server where it will be stored.
I need to read excel file data when upload the file .
Is there any way that can use html5 to read excel file data when file uploading in client side??
heard about
var reader = new FileReader();
any way that we can use this
Referring to #mituw16 comment on the question, take a look at the following question:
How to parse Excel file in Javascript/HTML5
As a seperate response to work from, I would suggest using a plugin like:
https://github.com/SheetJS/js-xlsx
To iterate through the spreadsheet and save the information in your database.
Working in Chrome, loading a local html or JS file.
I found many examples of how to load a file that is selected using the Choose File input.
However, didn't figure out how to do it given a file name without using the Choose File input.
The Choose File input returns a File object.
How to create the File object without the Choose File input?
From the File API:
new File(
Array parts,
String filename,
BlobPropertyBag properties
);
But didn't figure out what the parts and properties would be.
Edit: Use case:
I have code coverage results generated as part of a test suite. It is stored as JSON (which is easy to read), but I need to display it with the source code.
So the feature is to load the source code and JSON data, and render them together on a web page using HTML and Javascript.
The file would be opened from the browser and lives on the local machine. There is no server.
The browser cannot load arbitrary files by name from your filesystem without special extensions or other shenanigans. This is a security policy to prevent random web sites from reading files from your hard disk as you browse the internet.
If you're down to do something special like if you want to write a chrome app, you could get access to some nice APIs for accessing the filesystem:
https://developer.chrome.com/apps/fileSystem
The File constructor doesn't read a file from the harddrive, but rater make a virtual file, consider this:
var file = new File(["some", "content"], "/tmp/my-name.txt");
var reader = new FileReader();
reader.onload = function() {
console.log(reader.result); // somecontent
};
No file will be read or stored on the clients machine.
If you are talking about creating files in nodejs then you should take a look at fs.
For security reasons all browsers don't support predefined values on file fields so the answer is you can't.
I need to add a browse button inside my Chrome extension. Users click it and choose a file. I then want to retrieve the file contents (bytes) and do some work on it.
I don't want to have to submit the file to a remote server and then get the response (if that's even doable from inside a Chrome extension), it should be all client-side.
Is this doable inside Chrome extensions?
You should be looking at the FileReader API.
The FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.
A very good basic example of using this interface is in this question.
A minimal example: suppose that you have an <input type="file" id="file"> with a text file selected.
var file = document.getElementById("file").files[0];
var reader = new FileReader();
reader.onload = function(e){
console.log(e.target.result);
}
reader.readAsText(file);
If you need methods other than reading as text (i.e. binary data), see the docs.
Also, this is a good overview: Using files from web applications
Regarding your question it is totally feasible to load and process a file within an extension. I implemented it using message passing https://developer.chrome.com/docs/extensions/mv3/messaging/.
Here is an example of how you can implement it, in my case I used the input file to load an excel. This is my public repo.
https://github.com/juanmachuca95/gomeetplus
I know the syntax for saving files in Parse.com (https://www.parse.com/docs/js_guide#files-classes), but now I have a URL instead of a local file - http://graph.facebook.com/{FacebookID}/picture, how can I save it in Parse? Thanks!
You can't create/save a Parse File with the contents of a remote file, only data you have locally (like a base 64 encoded string, or a byte array, etc.)
See the Parse File (JavaScript) docs here: https://parse.com/docs/js_guide#files