Đây là một bài viết dài, nhưng tôi đã mệt mỏi với tất cả những ví dụ không hoạt động cho tôi vì họ đã sử dụng các đối tượng hứa hẹn hoặc một
0 sai lầm có ý nghĩa khác khi bạn đang sử dụng ReactJS. Việc thực hiện của tôi là sử dụng một dropzone với ReactJS và tôi đã nhận được các byte bằng cách sử dụng một khung tương tự như những gì được đăng tại trang web sau đây, khi không có gì khác ở trên: //www.mokuji.me/article/drop-upload- Hướng dẫn-1. Có 2 chìa khóa, đối với tôi:- Bạn phải lấy các byte từ đối tượng sự kiện, sử dụng và trong chức năng Onload của người làm phim.
Tôi đã thử các kết hợp khác nhau, nhưng cuối cùng, những gì đã hoạt động là:
1
Trong đó
2 là sự kiện. React yêu cầu
3, bạn có thể sử dụng
4 trong JavaScript đơn giản. Nhưng điều đó đã cho tôi chuỗi byte base64 được mã hóa.Vì vậy, tôi sẽ bao gồm các dòng áp dụng để tích hợp điều này như thể bạn đang sử dụng React, bởi vì đó là cách tôi xây dựng nó, nhưng hãy cố gắng khái quát điều này và thêm các bình luận khi cần thiết, để áp dụng cho một JavaScript Vanilla JavaScript Thực hiện - Hãy cẩn thận rằng tôi đã không sử dụng nó như thế trong một cấu trúc như vậy để kiểm tra nó.
Đây sẽ là các ràng buộc của bạn ở đầu, trong hàm tạo của bạn, trong một khung React [không liên quan đến việc triển khai JavaScript vani]:
this.uploadFile = this.uploadFile.bind[this];
this.processFile = this.processFile.bind[this];
this.errorHandler = this.errorHandler.bind[this];
this.progressHandler = this.progressHandler.bind[this];
Và bạn sẽ có
5 trong phần tử dropzone của mình. Nếu bạn đang làm điều này mà không phản ứng, đây là tương đương với việc thêm trình xử lý sự kiện Onclick bạn muốn chạy khi bạn nhấp vào nút "Tải lên tệp".
Sau đó, chức năng [các dòng áp dụng ... Tôi sẽ bỏ lại chỉ báo tiến trình tải lên của mình, v.v.]:
uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
Có câu hỏi về cú pháp đó, đối với Vanilla JS, về cách lấy đối tượng tệp đó:
JavaScript/HTML5/JQuery Drag-and-Drep Tải lên-"Loại không thể nhận được: Không thể đọc thuộc tính 'Tệp' của không xác định"
Lưu ý rằng Dropzone của React sẽ đặt đối tượng tệp vào
6 cho bạn, miễn là bạn thêm
7 vào
8 của bạn vào hàm tạo của bạn. Tôi đã thêm cú pháp từ một câu trả lời trên bài đăng đó về cách lấy đối tượng tệp của bạn. Nó nên hoạt động, hoặc có những bài viết khác ở đó có thể giúp đỡ. Nhưng tất cả những gì Q/A đã nói với tôi là làm thế nào để có được đối tượng
9, chứ không phải chính dữ liệu blob. Và ngay cả khi tôi đã làm uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
0 như trong câu trả lời của Sebu, điều này không bao gồm
4 với nó vì một số lý do, nó không cho tôi biết cách đọc nội dung của Blob và cách thực hiện mà không có đối tượng hứa hẹn. Vì vậy, đó là nơi người quay phim đến, mặc dù tôi thực sự đã thử và thấy tôi không thể sử dụng uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
2 của họ để có được.Bạn sẽ phải có các chức năng khác đi cùng với cấu trúc này - một để xử lý
uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
3, một cho uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
4 [cả hai đều được hiển thị xa hơn bên dưới], và sau đó Trong dòng cuối cùng đó. Về cơ bản, bạn đang chuyển thẳng uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
7 của mình vào chức năng uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
5 đó, từ những gì tôi có thể nói.Vì vậy, phương thức
uploadFile[event]{
// This is for React, only
this.setState[{
files: event,
}];
console.log['File count: ' + this.state.files.length];
// You might check that the "event" has a file & assign it like this
// in vanilla Javascript:
// var files = event.target.files;
// if [!files && files.length > 0]
// files = [event.dataTransfer ? event.dataTransfer.files :
// event.originalEvent.dataTransfer.files];
// You cannot use "files" as a variable in React, however:
const in_files = this.state.files;
// iterate, if files length > 0
if [in_files.length > 0] {
for [let i = 0; i < in_files.length; i++] {
// use this, instead, for vanilla JS:
// for [var i = 0; i < files.length; i++] {
const a = i + 1;
console.log['in loop, pass: ' + a];
const f = in_files[i]; // or just files[i] in vanilla JS
const reader = new FileReader[];
reader.onerror = this.errorHandler;
reader.onprogress = this.progressHandler;
reader.onload = this.processFile[f];
reader.readAsDataURL[f];
}
}
}
5 gọi hàm processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
0 của tôi [chỉ các dòng áp dụng, chỉ]:processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
Và
processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
1 nên có base64 byte.Chức năng bổ sung:
errorHandler[e]{
switch [e.target.error.code] {
case e.target.error.NOT_FOUND_ERR:
alert['File not found.'];
break;
case e.target.error.NOT_READABLE_ERR:
alert['File is not readable.'];
break;
case e.target.error.ABORT_ERR:
break; // no operation
default:
alert['An error occurred reading this file.'];
break;
}
}
progressHandler[e] {
if [e.lengthComputable]{
const loaded = Math.round[[e.loaded / e.total] * 100];
let zeros = '';
// Percent loaded in string
if [loaded >= 0 && loaded < 10] {
zeros = '00';
}
else if [loaded < 100] {
zeros = '0';
}
// Display progress in 3-digits and increase bar length
document.getElementById["progress"].textContent = zeros + loaded.toString[];
document.getElementById["progressBar"].style.width = loaded + '%';
}
}
Và đánh dấu chỉ số tiến độ áp dụng: đánh dấu:
000%
Và CSS:
.progressBar {
background-color: rgba[255, 255, 255, .1];
width: 100%;
height: 26px;
}
#progressBar {
background-color: rgba[87, 184, 208, .5];
content: '';
width: 0;
height: 26px;
}
EPILOGUE:
Bên trong
processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
0, vì một số lý do, tôi không thể thêm processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
1 vào một biến mà tôi đã khắc trong processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
4. Vì vậy, thay vào đó, tôi đặt nó trực tiếp thành biến, processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
5, có trong đối tượng JSON của tôi, processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
6 - đối tượng giống như processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
4 của tôi đang sử dụng. processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
5 là một mảng để tôi có thể đẩy nhiều tệp. Nó đã như thế này: const fileArray = [];
// Collect any existing attachments
if [RequestForm.state.attachments.length > 0] {
for [let i=0; i < RequestForm.state.attachments.length; i++] {
fileArray.push[RequestForm.state.attachments[i]];
}
}
// Add the new one to this.state
fileArray.push[bytes];
// Update the state
RequestForm.setState[{
attachments: fileArray,
}];
Sau đó, vì
processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
4 đã chứa processFile[theFile] {
return function[e] {
const bytes = e.target.result.split['base64,'][1];
}
}
6:this.stores = [
RequestForm,
]
Tôi có thể tham khảo nó như
errorHandler[e]{
switch [e.target.error.code] {
case e.target.error.NOT_FOUND_ERR:
alert['File not found.'];
break;
case e.target.error.NOT_READABLE_ERR:
alert['File is not readable.'];
break;
case e.target.error.ABORT_ERR:
break; // no operation
default:
alert['An error occurred reading this file.'];
break;
}
}
progressHandler[e] {
if [e.lengthComputable]{
const loaded = Math.round[[e.loaded / e.total] * 100];
let zeros = '';
// Percent loaded in string
if [loaded >= 0 && loaded < 10] {
zeros = '00';
}
else if [loaded < 100] {
zeros = '0';
}
// Display progress in 3-digits and increase bar length
document.getElementById["progress"].textContent = zeros + loaded.toString[];
document.getElementById["progressBar"].style.width = loaded + '%';
}
}
1 từ đó trở đi. Tính năng React không áp dụng trong vani JS. Bạn có thể xây dựng một cấu trúc tương tự trong JavaScript đơn giản với một biến toàn cầu và theo đó, theo đó, tuy nhiên, dễ dàng hơn nhiều:var fileArray = new Array[]; // place at the top, before any functions
// Within your processFile[]:
var newFileArray = [];
if [fileArray.length > 0] {
for [var i=0; i < fileArray.length; i++] {
newFileArray.push[fileArray[i]];
}
}
// Add the new one
newFileArray.push[bytes];
// Now update the global variable
fileArray = newFileArray;
Sau đó, bạn luôn chỉ tham khảo
errorHandler[e]{
switch [e.target.error.code] {
case e.target.error.NOT_FOUND_ERR:
alert['File not found.'];
break;
case e.target.error.NOT_READABLE_ERR:
alert['File is not readable.'];
break;
case e.target.error.ABORT_ERR:
break; // no operation
default:
alert['An error occurred reading this file.'];
break;
}
}
progressHandler[e] {
if [e.lengthComputable]{
const loaded = Math.round[[e.loaded / e.total] * 100];
let zeros = '';
// Percent loaded in string
if [loaded >= 0 && loaded < 10] {
zeros = '00';
}
else if [loaded < 100] {
zeros = '0';
}
// Display progress in 3-digits and increase bar length
document.getElementById["progress"].textContent = zeros + loaded.toString[];
document.getElementById["progressBar"].style.width = loaded + '%';
}
}
2, liệt kê nó cho bất kỳ chuỗi byte nào, ví dụ: errorHandler[e]{
switch [e.target.error.code] {
case e.target.error.NOT_FOUND_ERR:
alert['File not found.'];
break;
case e.target.error.NOT_READABLE_ERR:
alert['File is not readable.'];
break;
case e.target.error.ABORT_ERR:
break; // no operation
default:
alert['An error occurred reading this file.'];
break;
}
}
progressHandler[e] {
if [e.lengthComputable]{
const loaded = Math.round[[e.loaded / e.total] * 100];
let zeros = '';
// Percent loaded in string
if [loaded >= 0 && loaded < 10] {
zeros = '00';
}
else if [loaded < 100] {
zeros = '0';
}
// Display progress in 3-digits and increase bar length
document.getElementById["progress"].textContent = zeros + loaded.toString[];
document.getElementById["progressBar"].style.width = loaded + '%';
}
}
3 cho tệp đầu tiên.