fileto
v0.1.2
Published
A little library to work with Javascript File objects
Downloads
11
Readme
fileto
A little library to work with Javascript File objects.
This repository was created for use by CDC programs to collaborate on public health surveillance related projects in support of the CDC Surveillance Strategy. Github is not hosted by the CDC, but is used by CDC and its partners to share information and collaborate on software.
impetus
Suppose we have an HTML file input, like so:
<input type="file" id="fileinput" />
Now, let's get the corresponding File object in Javascript.
var file = document.getElementById("fileinput");
What can we do with this? We can use its metadata, or we can read it with a FileReader, and not a lot more than that. More importantly, if we change the contents of the input, the file object goes *poof!* and ceases to be. In a complex, file-oriented web application, this may be extremely undesirable. So, to solve this, we can stash away the contents of the file. fileto
is a little library to make that a little bit easier.
functions
Suppose we load a textfile called test.txt
into the above file input. Its contents are:
foobar
To get the actual File object, we'll need to listen to the change event:
file.addEventListener("change", function(){ /* ...Our code here... */ });
Then fileto
can do the following:
fileto.object(<file>)
- Unwraps a File object.
fileto.object(this.files[0]);
/* {
"name":"test.txt",
"lastModified":1544812259347,
"size":7,
"type":"text/plain"
} */
Note that the return is a plain Object, rather than a File object. This is useful if you need to track a File's metadata after you may no longer have access to the File (e.g., the File Input has already been reused).
fileto.string(<file>)
- Unwraps a File object and returns its contents as a JSON string. Literally just a wrapper for JSON.stringify(fileto.object(<file>))
fileto.string(this.files[0]);
/* '{"name":"test.txt","lastModified":1544812259347,"size":7,"type":"text/plain"}' */
fileto.callback(<file>, callback, readAs)
- Unwraps a File Object, reads its contents, and executes a callback. The unwrapped file Object will be passed to the callback, and its contents will be in file.contents
.
fileto.callback(this.files[0], function(output){
console.log(output);
});
/* {
contents: "foobar"
lastModified: 1544812259347
name: "test.txt"
size: 7
type: "text/plain"
} */
By default, readAs
is Text
, but can be set to ArrayBuffer
, BinaryString
, DataURL
, or Text
.
fileto.promise(<file>, readAs)
- Returns a promise to unwrap a File Object, read the file's contents, and resolve the promise with the unwrapped Object. The contents will be in response.contents
.
fileto.promise(this.files[0]).then(function(output){
console.log(output);
});
/* {
contents: "foobar"
lastModified: 1544812259347
name: "test.txt"
size: 7
type: "text/plain"
} */
If Promises aren't available, returns an object with a then
function that executes a callback as though it were a promise, and returns an object with a catch attribute pointing to a null function. Basically, it's a hack to make this thing work under 90% of the circumstances that people use promises for. Note that while this does not enable chaining. If you need Promises for users of terrible browsers, Polyfill them.
To see each of these in action, check out the demo.
Public Domain
This repository constitutes a work of the United States Government and is not subject to domestic copyright protection under 17 USC § 105. This repository is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the CC0 1.0 Universal public domain dedication. All contributions to this repository will be released under the CC0 dedication. By submitting a pull request you are agreeing to comply with this waiver of copyright interest.
License
The repository utilizes code licensed under the terms of the Apache Software License and therefore is licensed under ASL v2 or later.
This source code in this repository is free: you can redistribute it and/or modify it under the terms of the Apache Software License version 2, or (at your option) any later version.
This source code in this repository is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache Software License for more details.
You should have received a copy of the Apache Software License along with this program. If not, see http://www.apache.org/licenses/LICENSE-2.0.html
The source code forked from other open source projects will inherit its license.
Privacy
This repository contains only non-sensitive, publicly available data and information. All material and community participation is covered by the Surveillance Platform Disclaimer and Code of Conduct. For more information about CDC's privacy policy, please visit http://www.cdc.gov/privacy.html.
Contributing
Anyone is encouraged to contribute to the repository by forking and submitting a pull request. (If you are new to GitHub, you might start with a basic tutorial.) By contributing to this project, you grant a world-wide, royalty-free, perpetual, irrevocable, non-exclusive, transferable license to all users under the terms of the Apache Software License v2 or later.
All comments, messages, pull requests, and other submissions received through CDC including this GitHub page are subject to the Presidential Records Act and may be archived. Learn more at http://www.cdc.gov/other/privacy.html.
Records
This repository is not a source of government records, but is a copy to increase collaboration and collaborative potential. All government records will be published through the CDC web site.
Notices
Please refer to CDC's Template Repository for more information about contributing to this repository, public domain notices and disclaimers, and code of conduct.