Javascript và tệp tải lên hướng dẫn mã javascript để xử lý các tệp tải lên. Như kiểm tra kiểu tệp, kích thước, số lượng tệp…
Nhập thẻ tạo tệp tải lên
Khi cần tạo 1 điều khiển để người xem chọn tải lên tệp. You will used tag input with type is file. Xem ví dụ sau
Thuộc tính html cho file upload
Sau đây là một số thuốc tính html cần quan tâm và sử dụng để thiết lập chương trình javascript tương tác với tệp tải lên
Thuộc tính mô tả name Tên của đầu vào, dùng khi gửi idTên duy nhất định danh thẻ đầu vào, dùng trong css, jsmultipleCho phép người dùng chọn nhiều tệp tệpMảng các tệp mà người dùng chọn. Mỗi phần tử là 1 tệp có các thông tin. name, size, type, lastModified, lastModifiedDateacceptLiệt kê các loại tệp cho phép người dùng chọn trong hộp thoại chọn tệpCác sự kiện của tệp tải lên
Hai sự kiện hỗ trợ là thay đổi đầu vào. Sự kiện thay đổi thường được sử dụng để chạy hàm javascript xử lý khi người dùng chọn tệp
Ví dụ sử dụng javascript và tải lên tệp
Chọn file function xulyfile[]{ var arr = event.target.files;//mảng các file được chọn var f = arr[0]; console.log[f]; }storage_download_via_url.js
Phiên bản web 8
________số 8Tải xuống dữ liệu trực tiếp từ SDK
Từ phiên bản 9. 5 trở lên, SDK cung cấp các chức năng này để tải xuống trực tiếp
Sử dụng các chức năng này, bạn có thể bỏ qua việc tải xuống từ một URL và thay vào đó trả về dữ liệu trong mã của bạn. Điều này cho phép kiểm soát truy cập chi tiết hơn thông qua Quy tắc bảo mật Firebase
Ghi chú.// Create a reference with an initial file path and name var storage = firebase.storage[]; var pathReference = storage.ref['images/stars.jpg']; // Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL['gs://bucket/images/stars.jpg']; // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! var httpsReference = storage.refFromURL['//firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg'];2 chỉ khả dụng cho Nút. js vàdownload-files.js
// Create a reference with an initial file path and name var storage = firebase.storage[]; var pathReference = storage.ref['images/stars.jpg']; // Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL['gs://bucket/images/stars.jpg']; // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! var httpsReference = storage.refFromURL['//firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg'];0 chỉ khả dụng cho các môi trường giống như trình duyệt.download-files.js
Cấu hình CORS
Để tải xuống dữ liệu trực tiếp trong trình duyệt, bạn phải định cấu hình bộ chứa Lưu trữ đám mây của mình để truy cập trên nhiều nguồn gốc [CORS]. Điều này có thể được thực hiện với công cụ dòng lệnh
// Create a reference with an initial file path and name var storage = firebase.storage[]; var pathReference = storage.ref['images/stars.jpg']; // Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL['gs://bucket/images/stars.jpg']; // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! var httpsReference = storage.refFromURL['//firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg'];5 mà bạn có thể cài đặt từ đâydownload-files.js
Nếu bạn không muốn có bất kỳ hạn chế nào dựa trên miền [trường hợp phổ biến nhất], hãy sao chép JSON này vào một tệp có tên
// Create a reference with an initial file path and name var storage = firebase.storage[]; var pathReference = storage.ref['images/stars.jpg']; // Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL['gs://bucket/images/stars.jpg']; // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! var httpsReference = storage.refFromURL['//firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg'];6download-files.js
Chọn file function xulyfile[]{ var arr = event.target.files;//mảng các file được chọn var f = arr[0]; console.log[f]; }3
Chạy
// Create a reference with an initial file path and name var storage = firebase.storage[]; var pathReference = storage.ref['images/stars.jpg']; // Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL['gs://bucket/images/stars.jpg']; // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! var httpsReference = storage.refFromURL['//firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg'];7 để triển khai các hạn chế nàydownload-files.js
Để biết thêm thông tin, hãy tham khảo tài liệu Google Cloud Storage
Xử lý lỗi
Có một số lý do khiến lỗi có thể xảy ra khi tải xuống, bao gồm tệp không tồn tại hoặc người dùng không có quyền truy cập tệp mong muốn. Bạn có thể tìm thêm thông tin về lỗi trong phần Xử lý lỗi của tài liệu