Home  >  Article  >  Web Front-end  >  Implementation code for JavaScript to directly operate local files_javascript skills

Implementation code for JavaScript to directly operate local files_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:40:49921browse

Recently, Mozilla submitted a draft to the W3C, aiming to launch a JavaScript API that allows JavaScript scripts to directly operate local files. The latest browser version of Firefox already provides a lot of support for this API. However, due to security concerns, this API Only limited access to local files will be provided.
This API introduces a FileReader object that can asynchronously load files in the local file system and allows developers to assign callback functions for events such as loading failure, progress, loading completion, etc. It provides methods to read from local files Get text or binary data and interrupt the operation at any time through an abort method.

The FileReader object makes client-side file access a reality. Users no longer have to upload files to the server for processing as before. In fact, IE and Firefox already have some non-standard methods to access local files, but these Methods are not standard JavaScript behavior.

The FileReader object does not require special permissions, but for obvious security reasons, direct access using the file path is not allowed. It must be combined with the regular HTML file object. After the user clicks to browse, select the local file , JavaScript then references the file through the DOM and operates it. This mechanism makes the API relatively safe, because obtaining any local file requires manual participation by the user.

The following example uses this API to open a local file and display the file content in a div container. We can even add the contentEditable attribute to the div so that the user can directly edit the content of the file. However, this API does not There is no method provided to write the file contents back to a local file, so your edited content cannot be saved.

Although adding APIs to JavaScript is not a major technological revolution, it will expand the horizons of Web applications and allow the Web platform to implement more and more functions.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn