Javascript bao gồm tệp văn bản

HTML 5 cung cấp một cách tiêu chuẩn để tương tác với các tệp cục bộ với sự trợ giúp của API tệp. API tệp cho phép tương tác với các tệp đơn, nhiều tệp cũng như BLOB. API FileReader có thể được sử dụng để đọc tệp không đồng bộ khi cộng tác với xử lý sự kiện JavaScript. Tuy nhiên, tất cả các trình duyệt không hỗ trợ HTML 5, vì vậy điều quan trọng là phải kiểm tra tính tương thích của trình duyệt trước khi sử dụng API tệp. Có bốn phương thức sẵn có trong API FileReader để đọc các tệp cục bộ

  • Trình đọc tệp. readAsArrayBuffer[]. Đọc nội dung của tệp đầu vào được chỉ định. Thuộc tính kết quả chứa ArrayBuffer đại diện cho dữ liệu của tệp
  • Trình đọc tệp. readAsBinaryString[]. Đọc nội dung của tệp đầu vào được chỉ định. Thuộc tính kết quả chứa dữ liệu nhị phân thô từ tệp dưới dạng chuỗi
  • Trình đọc tệp. readAsDataURL[]. Đọc nội dung của tệp đầu vào được chỉ định. Thuộc tính kết quả chứa một URL đại diện cho dữ liệu của tệp
  • Trình đọc tệp. readAsText[]. Đọc nội dung của tệp đầu vào được chỉ định. Thuộc tính kết quả chứa nội dung của tệp dưới dạng chuỗi văn bản. Phương thức này có thể lấy phiên bản mã hóa làm đối số thứ hai [nếu được yêu cầu]. Mã hóa mặc định là UTF-8

Trong trường hợp này, chúng tôi đang sử dụng FileReader. readAsText[] để đọc cục bộ. tập tin txt

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
7

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
8

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
9

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
0
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
1

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
2

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
3

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
0
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
5
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
6
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
7
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
8

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
9
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
30
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
8
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
32

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
0
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
34

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
0____136
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
37
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
38

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
0____230
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
31
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
32

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
33
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
34____235
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
36

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
9
fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
38____239
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
30
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
31
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
32

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
9
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
34
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
35
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
36
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
37

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
9
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
39____331
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
41

_______442____234____444

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
36

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
46
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
47

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
9
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
49

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
9
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
41____542
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
43

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
33
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
45

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
0
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
47

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
48

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
49

Mã này in nội dung của tệp đầu vào giống hệt như trong tệp đầu vào

JavaScript được biết đến nhiều nhất để phát triển trang web nhưng nó cũng được sử dụng trong nhiều môi trường không có trình duyệt. Bạn có thể học JavaScript từ đầu bằng cách làm theo Hướng dẫn JavaScript và Ví dụ về JavaScript này


Tôi đang cố triển khai một trình đọc tệp văn bản đơn giản bằng cách tạo một hàm nhận đường dẫn của tệp và chuyển đổi từng dòng văn bản thành một mảng ký tự, nhưng nó không hoạt động

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
3

Điều gì đang xảy ra ở đây?

Điều này dường như vẫn không hoạt động sau khi thay đổi mã một chút so với bản sửa đổi trước đó và bây giờ nó mang lại cho tôi một ngoại lệ ________ 6400 101

Tôi đã thử nghiệm điều này trên Firefox và nó hoạt động, nhưng trong Google Chrome thì nó không hoạt động và nó liên tục mang lại cho tôi Ngoại lệ 101. Làm cách nào tôi có thể làm cho điều này hoạt động trên không chỉ Firefox mà còn trên các trình duyệt khác [đặc biệt là Chrome]?

ggorlen

36. 8k7 huy hiệu vàng61 huy hiệu bạc76 huy hiệu đồng

đã hỏi ngày 21 tháng 1 năm 2013 lúc 20. 14

3

Bạn cần kiểm tra trạng thái 0 [vì khi tải tệp cục bộ bằng

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
400, bạn không nhận được trạng thái trả về vì đó không phải từ
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
402]

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
4

Và chỉ định

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
403 trong tên tệp của bạn

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
9

Themroc

4752 huy hiệu bạc5 huy hiệu đồng

đã trả lời 21 tháng 1 năm 2013 lúc 20. 20

Majid Laissi Majid Laissi

18. 6k18 huy hiệu vàng65 huy hiệu bạc105 huy hiệu đồng

29

Sau khi giới thiệu về api tìm nạp trong javascript, việc đọc nội dung tệp không thể đơn giản hơn

đọc một tập tin văn bản

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file

đọc một tập tin json

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json

Cập nhật 30/07/2018 [miễn trừ trách nhiệm]

Kỹ thuật này hoạt động tốt trong Firefox, nhưng có vẻ như triển khai

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
404 của Chrome không hỗ trợ lược đồ URL
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
405 vào ngày viết bản cập nhật này [đã thử nghiệm trong Chrome 68]

Cập nhật-2 [từ chối trách nhiệm]

Kỹ thuật này không hoạt động với Firefox trên phiên bản 68 [ngày 9 tháng 7 năm 2019] vì lý do [bảo mật] tương tự như Chrome.

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
406. Xem https. // nhà phát triển. mozilla. org/en-US/docs/Web/HTTP/CORS/Lỗi/CORSRequestNotHttp

OlivierM

2.57223 huy hiệu bạc35 huy hiệu đồng

đã trả lời 9 tháng 9 năm 2017 lúc 9. 42

11

Truy cập Javascript. Và đi đến phần readAsText và thử ví dụ. Bạn sẽ có thể biết chức năng readAsText của FileReader hoạt động như thế nào

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
3____23

Teocci

5.7121 huy hiệu vàng42 huy hiệu bạc42 huy hiệu đồng

đã trả lời 20 tháng 3 năm 2015 lúc 21. 30

Amit Chaurasia Amit Chaurasia

1.4092 huy hiệu vàng11 huy hiệu bạc8 huy hiệu đồng

3

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
3____44

đã trả lời 22 tháng 8 năm 2017 lúc 10. 46

3

giải pháp hiện đại

Sử dụng

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
407 như sau

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
4

Khi người dùng tải lên một tệp văn bản thông qua đầu vào đó, nó sẽ được ghi vào bảng điều khiển. Đây là bản demo jsbin đang hoạt động

Đây là một phiên bản dài dòng hơn

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
40

Hiện tại [tháng 1 năm 2020] tính năng này chỉ hoạt động trong Chrome và Firefox, hãy kiểm tra tính tương thích tại đây nếu bạn sẽ đọc nội dung này trong tương lai. https. // nhà phát triển. mozilla. org/en-US/docs/Web/API/Blob/text

Trên các trình duyệt cũ hơn, điều này sẽ hoạt động

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
41

Có liên quan. Kể từ tháng 9 năm 2020, API Hệ thống tệp gốc mới có sẵn trong Chrome và Edge trong trường hợp bạn muốn có quyền truy cập đọc vĩnh viễn [và thậm chí cả quyền ghi] đối với tệp do người dùng chọn

2

Có, JS có thể đọc các tệp cục bộ [xem FileReader[]] nhưng không tự động. người dùng phải chuyển tệp hoặc danh sách tệp vào tập lệnh bằng html

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
408

Sau đó, với JS, có thể xử lý [xem ví dụ] tệp hoặc danh sách tệp, một số thuộc tính của chúng và tệp hoặc nội dung tệp

Điều mà JS không thể làm vì lý do bảo mật là truy cập tự động [không cần người dùng nhập] vào hệ thống tệp trên máy tính của anh ấy

Để cho phép JS tự động truy cập vào fs cục bộ, cần tạo không phải tệp html có JS bên trong mà là tài liệu hta

Một tệp hta có thể chứa JS hoặc VBS bên trong nó

Nhưng tệp thực thi hta sẽ chỉ hoạt động trên hệ thống windows

Đây là hành vi tiêu chuẩn của trình duyệt

Ngoài ra, Google Chrome đã hoạt động tại API fs, thêm thông tin tại đây. http. //www. html5rocks. com/vi/tutorials/file/filesystem/

gỗ sồi

54. 4k15 huy hiệu vàng105 huy hiệu bạc147 huy hiệu đồng

đã trả lời 29 tháng 1 năm 2016 lúc 3. 31

Chim sẻ Chim sẻ

3313 huy hiệu bạc3 huy hiệu đồng

0

Sử dụng chức năng Tìm nạp và không đồng bộ

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
42

đã trả lời 8 tháng 1 năm 2019 lúc 13. 15

barro32 barro32

2.20420 huy hiệu bạc34 huy hiệu đồng

2

Hãy thử tạo hai chức năng

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
43

chuông hóa đơn

20. 4k5 huy hiệu vàng42 huy hiệu bạc57 huy hiệu đồng

đã trả lời 16 tháng 10 năm 2013 lúc 23. 29

1

Chắc bạn dùng thử rồi, gõ "false" như sau

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
44

A-Sharabani

16. 3k16 huy hiệu vàng106 huy hiệu bạc125 huy hiệu đồng

đã trả lời 26/11/2013 lúc 12. 17

khoảnh khắc khoảnh khắc

1291 huy hiệu bạc3 huy hiệu đồng

ví dụ khác - trình đọc của tôi với lớp FileReader

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
45

đã trả lời 19 tháng 2 năm 2015 lúc 15. 35

websky websky

2.9421 huy hiệu vàng33 huy hiệu bạc29 huy hiệu đồng

1

Điều này có thể giúp,

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
46

đã trả lời 18/11/2016 lúc 15. 20

Sameera R. Sameera R.

4.2161 huy hiệu vàng33 huy hiệu bạc51 huy hiệu đồng

Lệnh gọi AJAX cục bộ trong Chrome không được hỗ trợ do chính sách cùng nguồn gốc

Thông báo lỗi trên chrome là như thế này. "Yêu cầu nguồn gốc chéo không được hỗ trợ cho các sơ đồ giao thức. http, dữ liệu, chrome, tiện ích chrome, https. "

Điều này có nghĩa là chrome tạo một đĩa ảo cho mọi miền để giữ các tệp do miền cung cấp bằng giao thức http/https. Mọi quyền truy cập vào các tệp bên ngoài đĩa ảo này đều bị hạn chế theo chính sách gốc. Các yêu cầu và phản hồi AJAX xảy ra trên http/https, do đó sẽ không hoạt động đối với các tệp cục bộ

Firefox không đặt hạn chế như vậy, do đó mã của bạn sẽ hoạt động vui vẻ trên Firefox. Tuy nhiên, cũng có cách giải quyết cho chrome. xem ở đây

đã trả lời 26 tháng 12 năm 2018 lúc 12. 12

1

Thêm vào một số câu trả lời ở trên, giải pháp sửa đổi này phù hợp với tôi

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
47

....

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
48

....

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
49

đã trả lời 31/12/2018 lúc 19. 50

Fabii Fabii

3.74013 huy hiệu vàng49 huy hiệu bạc89 huy hiệu đồng

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
90

- đọc văn bản tệp từ javascript
- Văn bản nhật ký bảng điều khiển từ tệp bằng javascript
- Google chrome và mozilla firefox

trong trường hợp của tôi, tôi có cấu trúc tệp này

Bàn điều khiển. nhật ký kết quả

đã trả lời 18 tháng 1 năm 2019 lúc 9. 38

nadir hamidou nadir hamidou

4232 huy hiệu vàng7 huy hiệu bạc18 huy hiệu đồng

1

Nếu bạn muốn nhắc người dùng chọn một tệp, hãy đọc nội dung của nó

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
91

Cách sử dụng

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
92

đã trả lời 16/12/2020 lúc 15. 13

yaya yaya

6.7891 huy hiệu vàng32 huy hiệu bạc34 huy hiệu đồng

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
93

đã trả lời 22 tháng 2 năm 2018 lúc 7. 41

Nhận dữ liệu tệp cục bộ trong js [dữ liệu. js] tải

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
94

tệp dữ liệu. js thích

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
95

động unixTime queryString ngăn lưu trữ

AJ hoạt động trên web http. //

đã trả lời 26 tháng 4 năm 2019 lúc 5. 20

1

Làm thế nào để đọc một tập tin cục bộ?

Bằng cách sử dụng này, bạn sẽ tải một tệp bằng loadText[] sau đó JS sẽ đợi không đồng bộ cho đến khi tệp được đọc và tải sau đó nó sẽ thực thi hàm readText[] cho phép bạn tiếp tục với logic JS bình thường của mình [bạn cũng có thể viết lệnh try catch

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
96

đã trả lời 3 tháng 10 năm 2019 lúc 13. 33

D. Chụp D. Chụp

1.5441 huy hiệu vàng20 huy hiệu bạc14 huy hiệu đồng

1

Hàm này được tạo cho trình duyệt và mở hộp thoại bộ chọn tệp và sau khi người dùng chọn, hãy đọc tệp dưới dạng nhị phân và gọi hàm gọi lại với dữ liệu đã đọc

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
97

Và sử dụng nó như thế này

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
98

đã trả lời 19 tháng 12 năm 2021 lúc 8. 24

MSS MSS

3.36423 huy hiệu bạc27 huy hiệu đồng

1

Bạn có thể nhập thư viện của tôi

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
99

sau đó, hàm

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
409 sẽ trả về tệp đã tải lên

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
0

Xin lưu ý. trên Google Chrome nếu mã HTML là cục bộ, lỗi sẽ xuất hiện nhưng việc lưu mã HTML và các tệp trực tuyến sau đó chạy tệp HTML trực tuyến sẽ hoạt động

đã trả lời 3 tháng 1 năm 2019 lúc 23. 40

Để đọc văn bản tệp cục bộ thông qua

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
410 bằng chrome, trình duyệt chrome phải chạy với đối số
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
411 để cho phép JavaScript truy cập tệp cục bộ, sau đó bạn có thể đọc nó bằng cách sử dụng
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
412 như sau

fetch['file.txt']
  .then[response => response.text[]]
  .then[text => console.log[text]]
  // outputs the content of the text file
1

đã trả lời 7 tháng 1 năm 2019 lúc 9. 56

Ali Ezzat Odeh Ali Ezzat Odeh

2.0631 huy hiệu vàng16 huy hiệu bạc17 huy hiệu đồng

Đây là một câu hỏi cũ nhưng có hai ý chính mà chúng ta phải rõ ràng. Chúng ta muốn đọc toàn bộ tệp hay lấy từng dòng một?

Tèo em muốn lấy cả file xử lý sau

Được rồi, điều đó rất dễ dàng, chúng tôi chỉ cần gọi

fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
413 [hãy nhớ rằng
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
413 giả định rằng tệp được mã hóa thành
fetch['file.json']
  .then[response => response.json[]]
  .then[jsonResponse => console.log[jsonResponse]]     
   // outputs a javascript object from the parsed json
415] và xử lý tệp như thế này

Chủ Đề