Lấy giá trị từ tệp đầu vào php

Sử dụng API tệp, nội dung web có thể yêu cầu người dùng chọn các tệp cục bộ và sau đó đọc nội dung của các tệp đó. Lựa chọn này có thể được thực hiện bằng cách sử dụng phần tử HTML

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
4 hoặc bằng cách kéo và thả

Truy cập (các) tệp đã chọn

Hãy xem xét HTML này

<input type="file" id="input" multiple />

API tệp cho phép truy cập một đối tượng

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
5 chứa
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6 đại diện cho các tệp do người dùng chọn

Thuộc tính

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
7 trên phần tử
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
8 cho phép người dùng chọn nhiều tệp

Truy cập tệp được chọn đầu tiên bằng bộ chọn DOM cổ điển

const selectedFile = document.getElementById('input').files[0];

Truy cập (các) tệp đã chọn trong một sự kiện thay đổi

Cũng có thể (nhưng không bắt buộc) truy cập vào

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
5 thông qua sự kiện
const numFiles = fileList.length;
0. Bạn cần sử dụng
const numFiles = fileList.length;
1 để thêm trình lắng nghe sự kiện
const numFiles = fileList.length;
0, như thế này

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}

Nhận thông tin về (các) tệp đã chọn

Đối tượng

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
5 do DOM cung cấp liệt kê tất cả các tệp được người dùng chọn, mỗi tệp được chỉ định là đối tượng
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6. Bạn có thể xác định có bao nhiêu tệp mà người dùng đã chọn bằng cách kiểm tra giá trị của thuộc tính
const numFiles = fileList.length;
5 của danh sách tệp

________số 8

Các đối tượng

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6 riêng lẻ có thể được truy xuất bằng cách truy cập danh sách dưới dạng một mảng

<input type="file" id="input" multiple />
7

Vòng lặp này lặp qua tất cả các tệp trong danh sách tệp

Có ba thuộc tính được cung cấp bởi đối tượng

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6 chứa thông tin hữu ích về tệp

const numFiles = fileList.length;
8

Tên tệp dưới dạng chuỗi chỉ đọc. Đây chỉ là tên tệp và không bao gồm bất kỳ thông tin đường dẫn nào

const numFiles = fileList.length;
9

Kích thước của tệp tính bằng byte dưới dạng số nguyên 64 bit chỉ đọc

<input type="file" id="input" multiple />
70

Loại MIME của tệp dưới dạng chuỗi chỉ đọc hoặc

<input type="file" id="input" multiple />
71 nếu không xác định được loại

Thí dụ. Hiển thị (các) kích thước tệp

Ví dụ sau đây cho thấy khả năng sử dụng thuộc tính

const numFiles = fileList.length;
9

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
4

Sử dụng các phần tử đầu vào tệp ẩn bằng phương thức click()

Bạn có thể ẩn phần tử tệp

<input type="file" id="input" multiple />
73 xấu xí được thừa nhận và hiển thị giao diện của riêng bạn để mở bộ chọn tệp và hiển thị tệp hoặc tệp mà người dùng đã chọn. Bạn có thể làm điều này bằng cách tạo kiểu cho phần tử đầu vào bằng
<input type="file" id="input" multiple />
74 và gọi phương thức
<input type="file" id="input" multiple />
75 trên phần tử
<input type="file" id="input" multiple />
73

Hãy xem xét HTML này

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
9

Mã xử lý sự kiện

<input type="file" id="input" multiple />
77 có thể trông như thế này

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
1

Bạn có thể tạo kiểu cho nút mới để mở bộ chọn tệp theo ý muốn

Sử dụng phần tử nhãn để kích hoạt phần tử đầu vào tệp ẩn

Để cho phép mở bộ chọn tệp mà không cần sử dụng JavaScript (phương thức click()), có thể sử dụng phần tử

<input type="file" id="input" multiple />
78. Lưu ý rằng trong trường hợp này, không được ẩn phần tử đầu vào bằng cách sử dụng
<input type="file" id="input" multiple />
79 (hoặc
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
40), nếu không, nhãn sẽ không thể truy cập được bằng bàn phím. Thay vào đó, hãy sử dụng kỹ thuật ẩn trực quan

Hãy xem xét HTML này

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
5

và CSS này

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6

Không cần thêm mã JavaScript để gọi

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
41. Ngoài ra, trong trường hợp này, bạn có thể tạo kiểu cho thành phần nhãn theo ý muốn. Bạn cần cung cấp một gợi ý trực quan về trạng thái tiêu điểm của trường nhập ẩn trên nhãn của nó, có thể là đường viền như minh họa ở trên hoặc màu nền hoặc bóng hộp. (Tại thời điểm viết bài này, Firefox không hiển thị gợi ý trực quan này cho các phần tử
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
42. )

Chọn tệp bằng cách kéo và thả

Bạn cũng có thể cho phép người dùng kéo và thả tệp vào ứng dụng web của mình

Bước đầu tiên là thiết lập một khu vực thả. Chính xác phần nào trong nội dung của bạn sẽ chấp nhận sự sụt giảm có thể khác nhau tùy thuộc vào thiết kế của ứng dụng của bạn, nhưng việc tạo một phần tử nhận sự kiện giảm là dễ dàng

const selectedFile = document.getElementById('input').files[0];
0

Trong ví dụ này, chúng tôi đang chuyển phần tử có ID

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
43 vào vùng thả của chúng tôi. Điều này được thực hiện bằng cách thêm người nghe cho các sự kiện
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
44,
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
45 và
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
46

Chúng tôi thực sự không cần phải làm bất cứ điều gì với các sự kiện

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
44 và
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
45 trong trường hợp của chúng tôi, vì vậy cả hai chức năng này đều đơn giản. Họ chỉ dừng truyền bá sự kiện và ngăn hành động mặc định xảy ra

const selectedFile = document.getElementById('input').files[0];
1

Điều kỳ diệu thực sự xảy ra trong hàm

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
49

const selectedFile = document.getElementById('input').files[0];
2

Ở đây, chúng tôi truy xuất trường

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
90 từ sự kiện, kéo danh sách tệp ra khỏi đó rồi chuyển trường đó tới
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
91. Kể từ thời điểm này, việc xử lý các tệp là như nhau cho dù người dùng sử dụng phần tử
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
8 hay kéo và thả

Thí dụ. Hiển thị hình thu nhỏ của hình ảnh do người dùng chọn

Giả sử bạn đang phát triển trang web chia sẻ ảnh tuyệt vời tiếp theo và muốn sử dụng HTML để hiển thị bản xem trước hình thu nhỏ của hình ảnh trước khi người dùng thực sự tải chúng lên. Bạn có thể thiết lập phần tử đầu vào hoặc vùng thả của mình như đã thảo luận trước đó và yêu cầu họ gọi một hàm, chẳng hạn như hàm

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
91 bên dưới

const selectedFile = document.getElementById('input').files[0];
3

Ở đây, vòng lặp của chúng tôi xử lý các tệp do người dùng chọn sẽ xem xét thuộc tính

<input type="file" id="input" multiple />
70 của từng tệp để xem liệu loại MIME của nó có bắt đầu bằng chuỗi "
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
95"). Đối với mỗi tệp là một hình ảnh, chúng tôi tạo một phần tử
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
96 mới. CSS có thể được sử dụng để thiết lập bất kỳ đường viền hoặc bóng đẹp nào và để chỉ định kích thước của hình ảnh, do đó không cần phải thực hiện ở đây

Mỗi hình ảnh có lớp CSS

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
97 được thêm vào, giúp dễ dàng tìm thấy trong cây DOM. Chúng tôi cũng thêm thuộc tính
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
98 cho mỗi hình ảnh chỉ định
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6 cho hình ảnh; . Chúng tôi sử dụng
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
10 để thêm hình thu nhỏ mới vào khu vực xem trước của tài liệu của chúng tôi

Tiếp theo, chúng tôi thiết lập

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
11 để xử lý tải hình ảnh không đồng bộ và gắn nó vào phần tử
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
96. Sau khi tạo đối tượng
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
11 mới, chúng tôi thiết lập chức năng
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
14 của nó và sau đó gọi
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
15 để bắt đầu thao tác đọc trong nền. Khi toàn bộ nội dung của tệp hình ảnh được tải, chúng sẽ được chuyển đổi thành URL
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
16 được chuyển đến lệnh gọi lại
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
14. Việc triển khai quy trình này của chúng tôi đặt thuộc tính
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
19 của phần tử
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
96 thành hình ảnh được tải, dẫn đến hình ảnh xuất hiện trong hình thu nhỏ trên màn hình của người dùng

Sử dụng URL đối tượng

Các phương thức DOM

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
50 và
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
51 cho phép bạn tạo các chuỗi URL đơn giản có thể được sử dụng để tham chiếu bất kỳ dữ liệu nào có thể được tham chiếu bằng đối tượng DOM
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6, bao gồm các tệp cục bộ trên máy tính của người dùng

Khi bạn có một đối tượng

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6 mà bạn muốn tham chiếu bằng URL từ HTML, bạn có thể tạo một URL đối tượng cho nó như thế này

const selectedFile = document.getElementById('input').files[0];
4

URL đối tượng là một chuỗi xác định đối tượng

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6. Mỗi khi bạn gọi
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
50, một URL đối tượng duy nhất sẽ được tạo ngay cả khi bạn đã tạo một URL đối tượng cho tệp đó rồi. Mỗi trong số này phải được phát hành. Mặc dù chúng được giải phóng tự động khi tài liệu được tải xuống, nhưng nếu trang của bạn sử dụng chúng một cách linh hoạt, bạn nên giải phóng chúng một cách rõ ràng bằng cách gọi
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
51

const selectedFile = document.getElementById('input').files[0];
5

Thí dụ. Sử dụng URL đối tượng để hiển thị hình ảnh

Ví dụ này sử dụng URL đối tượng để hiển thị hình thu nhỏ của hình ảnh. Ngoài ra, nó còn hiển thị thông tin tệp khác bao gồm tên và kích thước của chúng

HTML trình bày giao diện trông như thế này

const selectedFile = document.getElementById('input').files[0];
6

Điều này thiết lập phần tử tệp

<input type="file" id="input" multiple />
73 của chúng tôi cũng như một liên kết gọi bộ chọn tệp (vì chúng tôi ẩn đầu vào tệp để ngăn giao diện người dùng kém hấp dẫn đó hiển thị). Điều này được giải thích trong phần Sử dụng các phần tử đầu vào tệp ẩn bằng phương thức click(), cũng như phương thức gọi bộ chọn tệp

Phương pháp

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
91 sau

const selectedFile = document.getElementById('input').files[0];
7

Điều này bắt đầu bằng cách tìm nạp URL của

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
59 với ID
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
60. Đây là khối mà chúng tôi sẽ chèn danh sách tệp của mình, bao gồm cả hình thu nhỏ

Nếu đối tượng

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
5 được chuyển đến
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
91 là
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
63, chúng tôi đặt HTML bên trong của khối để hiển thị "Không có tệp nào được chọn. ". Mặt khác, chúng tôi bắt đầu xây dựng danh sách tệp của mình, như sau

  1. Một phần tử danh sách không có thứ tự (
    const inputElement = document.getElementById("input");
    inputElement.addEventListener("change", handleFiles, false);
    function handleFiles() {
      const fileList = this.files; /* now you can work with the file list */
    }
    
    64) mới được tạo
  2. Phần tử danh sách mới được chèn vào khối
    const inputElement = document.getElementById("input");
    inputElement.addEventListener("change", handleFiles, false);
    function handleFiles() {
      const fileList = this.files; /* now you can work with the file list */
    }
    
    59 bằng cách gọi phương thức
    const inputElement = document.getElementById("input");
    inputElement.addEventListener("change", handleFiles, false);
    function handleFiles() {
      const fileList = this.files; /* now you can work with the file list */
    }
    
    10 của nó
  3. Đối với mỗi
    const inputElement = document.getElementById("input");
    inputElement.addEventListener("change", handleFiles, false);
    function handleFiles() {
      const fileList = this.files; /* now you can work with the file list */
    }
    
    6 trong
    const inputElement = document.getElementById("input");
    inputElement.addEventListener("change", handleFiles, false);
    function handleFiles() {
      const fileList = this.files; /* now you can work with the file list */
    }
    
    5 được đại diện bởi
    const inputElement = document.getElementById("input");
    inputElement.addEventListener("change", handleFiles, false);
    function handleFiles() {
      const fileList = this.files; /* now you can work with the file list */
    }
    
    69
    1. Tạo một phần tử danh sách mới (
      const selectedFile = document.getElementById('input').files[0];
      
      00) và chèn nó vào danh sách
    2. Tạo phần tử hình ảnh mới (
      const selectedFile = document.getElementById('input').files[0];
      
      01)
    3. Đặt nguồn của hình ảnh thành URL đối tượng mới đại diện cho tệp, sử dụng
      const inputElement = document.getElementById("input");
      inputElement.addEventListener("change", handleFiles, false);
      function handleFiles() {
        const fileList = this.files; /* now you can work with the file list */
      }
      
      50 để tạo URL blob
    4. Đặt chiều cao của hình ảnh thành 60 pixel
    5. Thiết lập trình xử lý sự kiện tải của hình ảnh để giải phóng URL đối tượng vì nó không còn cần thiết sau khi hình ảnh đã được tải. Điều này được thực hiện bằng cách gọi phương thức
      const inputElement = document.getElementById("input");
      inputElement.addEventListener("change", handleFiles, false);
      function handleFiles() {
        const fileList = this.files; /* now you can work with the file list */
      }
      
      51 và chuyển vào chuỗi URL đối tượng như được chỉ định bởi
      const selectedFile = document.getElementById('input').files[0];
      
      04
    6. Nối mục danh sách mới vào danh sách

Đây là bản demo trực tiếp của đoạn mã trên

Thí dụ. Tải lên tệp do người dùng chọn

Một điều khác bạn có thể muốn làm là cho phép người dùng tải tệp hoặc tệp đã chọn (chẳng hạn như hình ảnh được chọn bằng ví dụ trước) lên máy chủ. Điều này có thể được thực hiện không đồng bộ rất dễ dàng

Tạo các tác vụ tải lên

Tiếp tục với đoạn mã đã tạo hình thu nhỏ trong ví dụ trước, hãy nhớ lại rằng mọi hình ảnh thu nhỏ đều thuộc lớp CSS

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
97 với
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
6 tương ứng được đính kèm trong thuộc tính
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
98. Điều này cho phép chúng tôi chọn tất cả các hình ảnh mà người dùng đã chọn để tải lên bằng cách sử dụng
const selectedFile = document.getElementById('input').files[0];
08, như thế này

const selectedFile = document.getElementById('input').files[0];
8

Dòng 2 tìm nạp một

const selectedFile = document.getElementById('input').files[0];
09, được gọi là
const selectedFile = document.getElementById('input').files[0];
10, của tất cả các thành phần trong tài liệu với lớp CSS
const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
97. Trong trường hợp của chúng tôi, đây sẽ là tất cả các hình thu nhỏ của hình ảnh. Khi chúng tôi có danh sách đó, việc lướt qua nó và tạo một phiên bản
const selectedFile = document.getElementById('input').files[0];
12 mới cho mỗi. Mỗi trong số này xử lý tải lên tệp tương ứng

Xử lý quá trình tải lên cho một tập tin

Hàm

const selectedFile = document.getElementById('input').files[0];
12 chấp nhận hai đầu vào. một phần tử hình ảnh và một tệp để đọc dữ liệu hình ảnh

const selectedFile = document.getElementById('input').files[0];
9

Hàm

const selectedFile = document.getElementById('input').files[0];
14 được hiển thị ở trên tạo ra một throbber, được sử dụng để hiển thị thông tin về tiến độ, sau đó tạo một
const selectedFile = document.getElementById('input').files[0];
15 để xử lý việc tải dữ liệu lên

Trước khi thực sự chuyển dữ liệu, một số bước chuẩn bị được thực hiện

  1. Trình nghe
    const selectedFile = document.getElementById('input').files[0];
    
    17 tải lên của
    const selectedFile = document.getElementById('input').files[0];
    
    15 được thiết lập để cập nhật thông tin phần trăm mới cho bộ ghi hình để khi quá trình tải lên tiến triển, bộ ghi hình sẽ được cập nhật dựa trên thông tin mới nhất
  2. Trình xử lý sự kiện tải lên
    const selectedFile = document.getElementById('input').files[0];
    
    19 của
    const selectedFile = document.getElementById('input').files[0];
    
    15 được thiết lập để cập nhật thông tin tiến trình rung lên 100% để đảm bảo chỉ báo tiến trình thực sự đạt 100% (trong trường hợp có vấn đề về mức độ chi tiết trong quá trình này). Sau đó, nó loại bỏ cái đập vì nó không còn cần thiết nữa. Điều này làm cho rung động biến mất sau khi quá trình tải lên hoàn tất
  3. Yêu cầu tải lên tệp hình ảnh được mở bằng cách gọi phương thức
    const selectedFile = document.getElementById('input').files[0];
    
    21 của
    const selectedFile = document.getElementById('input').files[0];
    
    15 để bắt đầu tạo yêu cầu POST
  4. Loại MIME để tải lên được đặt bằng cách gọi hàm
    const selectedFile = document.getElementById('input').files[0];
    
    15
    const selectedFile = document.getElementById('input').files[0];
    
    23. Trong trường hợp này, chúng tôi đang sử dụng loại MIME chung;
  5. Đối tượng
    const inputElement = document.getElementById("input");
    inputElement.addEventListener("change", handleFiles, false);
    function handleFiles() {
      const fileList = this.files; /* now you can work with the file list */
    }
    
    11 được sử dụng để chuyển đổi tệp thành chuỗi nhị phân
  6. Cuối cùng, khi nội dung được tải, hàm
    const selectedFile = document.getElementById('input').files[0];
    
    15
    const selectedFile = document.getElementById('input').files[0];
    
    26 được gọi để tải nội dung của tệp lên

Xử lý không đồng bộ quá trình tải tệp lên

Ví dụ này, sử dụng PHP ở phía máy chủ và JavaScript ở phía máy khách, minh họa việc tải tệp lên không đồng bộ

const inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
  const fileList = this.files; /* now you can work with the file list */
}
0

Thí dụ. Sử dụng URL đối tượng để hiển thị PDF

URL đối tượng có thể được sử dụng cho những thứ khác ngoài hình ảnh. Chúng có thể được sử dụng để hiển thị các tệp PDF được nhúng hoặc bất kỳ tài nguyên nào khác mà trình duyệt có thể hiển thị

Làm cách nào để lấy giá trị văn bản đầu vào trong PHP?

Sử dụng siêu toàn cục $_POST hoặc $_GET của PHP để truy xuất giá trị của thẻ đầu vào thông qua tên của thẻ HTML .

Làm cách nào tôi có thể lưu trữ tệp đã tải lên của mình trong HTML?

Tạo biểu mẫu HTML .
<. LOẠI TÀI LIỆU html>
Chọn hình ảnh để tải lên
.
.

Biến tập lệnh PHP nào chỉ định đường dẫn của tệp sẽ được tải lên?

$_FILES['file']['tmp_name'] − tệp đã tải lên trong thư mục tạm thời trên máy chủ web. $_FILES['file']['name'] − tên thực của tệp đã tải lên. $_FILES['file']['size'] − kích thước tính bằng byte của tệp đã tải lên. $_FILES['file']['type'] − loại MIME của tệp đã tải lên

Làm cách nào để tạo tệp tải lên trong PHP?

Cách tiếp cận. Trong “php” của bạn. ini”, tìm kiếm tham số “file_uploads” và đặt thành “Bật” như được đề cập bên dưới. Trong “chỉ số. html”, kiểu mã hóa phải là dữ liệu nhiều phần/biểu mẫu và phương thức phải là POST