Technical-filereader interface used in graduation thesis

Source: Internet
Author: User

Used to read the file into memory and read the data in the file.

The FileReader interface provides an asynchronous API that allows you to access the file system asynchronously in the main thread of the browser, reading the data in the file

Method of FileReader interface
Method Name Parameters Description
Readasbinarystring File To read a file as a binary encoding
Readastext File,[encoding] To read a file as text
Readasdataurl File Read the file as Dataurl
Abort (none) Terminal Read operations

The method does not return a read result, regardless of the success or failure of the read, which is stored in the result property.

Events for the FileReader interface
Event Describe
Onabort Interrupt
OnError Error
Onloadstart Begin
OnProgress is reading
OnLoad Successfully read
Onloadend Read complete regardless of successful failure

Use of the FileReader interface

  1. <p>
  2. <label> Please select a file:</label>
  3. <input type="file" id="file" />
  4. <input type="button" value="read image" onclick="Readasdataurl ()" />//Read image
  5. <input type="button" value="read binary data" onclick="readasbinarystring ()" />//Read binary data
  6. <input type="button" value="read text file" onclick="Readastext ()" />//Read text file
  7. </P>
  8. <div id="result" name="result"></div>
  1. var result=document.getElementById ("result");
  2. var file=document.getElementById ("file");
  3. Determine if the browser supports the FileReader interface
  4. if (typeof FileReader = = ' undefined ') {
  5. result. innerhtml="<div> Browser does not support FileReader interface!  </div> ";
  6. Make the selection control not operational
  7. File.setattribute ("Disabled", "disabled");
  8. }
  9. function Readasdataurl () {
  10. Verify that the image file is
  11. var file = document.getElementById ("file"). Files[0];
  12. if (!/image\/\w+/.test (File.type)) {
  13. Alert ("See Clearly, this needs a picture!") ");
  14. return false;
  15. }
  16. var reader = new FileReader ();
  17. Read the file into the page as a data URL
  18. Reader.readasdataurl (file);
  19. reader.onload=function (e) {
  20. var result=document.getElementById ("result");
  21. Show Files
  22. result.innerhtml=' alt="" /> ";
  23. }
  24. }
  25. function readasbinarystring () {
  26. var file = document.getElementById ("file"). Files[0];
  27. var reader = new FileReader ();
  28. Read the file into the page in binary form
  29. reader.readasbinarystring (file);
  30. reader.onload=function (f) {
  31. var result=document.getElementById ("result");
  32. Show Files
  33. result.innerhtml=This.result;
  34. }
  35. }
  36. function Readastext () {
  37. var file = document.getElementById ("file"). Files[0];
  38. var reader = new FileReader ();
  39. Read the file into the page as text
  40. Reader.readastext (file);
  41. reader.onload=function (f) {
  42. var result=document.getElementById ("result");
  43. Show Files
  44. result.innerhtml=This.result;
  45. }
  46. }
  47. </Script>

Technical-filereader interface used in graduation thesis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.