Hướng dẫn what is binary data type in javascript? - kiểu dữ liệu nhị phân trong javascript là gì?

Thuộc tính responseType của đối tượng XMLHTTPrequest có thể được đặt để thay đổi loại phản hồi dự kiến ​​từ máy chủ. Các giá trị có thể là chuỗi trống (mặc định), "arraybuffer", "blob", "document",

const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
0 và
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
1. Thuộc tính
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
2 sẽ chứa cơ thể thực thể theo responseType, dưới dạng
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
4,
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
5,
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
6,
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
7 hoặc chuỗi. Đây là
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
8 nếu yêu cầu không hoàn thành hoặc không thành công.

Ví dụ này đọc một hình ảnh dưới dạng tệp nhị phân và tạo một mảng số nguyên không dấu 8 bit từ các byte thô. Lưu ý rằng điều này sẽ không giải mã được hình ảnh và đọc các pixel. Bạn sẽ cần một thư viện giải mã PNG cho điều đó.

const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "arraybuffer";

req.onload = (event) => {
  const arrayBuffer = req.response; // Note: not req.responseText
  if (arrayBuffer) {
    const byteArray = new Uint8Array(arrayBuffer);
    byteArray.forEach((element, index) => {
      // do something with each byte in the array
    });
  }
};

req.send(null);

Bạn cũng có thể đọc một tệp nhị phân dưới dạng

const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
5 bằng cách đặt chuỗi "blob" thành thuộc tính responseType.

const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();

Nhận dữ liệu nhị phân trong các trình duyệt cũ hơn

Hàm

function loadBinaryResource(url) {
  const req = new XMLHttpRequest();
  req.open("GET", url, false);

  // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType("text/plain; charset=x-user-defined");
  req.send(null);
  return req.status === 200 ? req.responseText : "";
}
2 được hiển thị bên dưới tải dữ liệu nhị phân từ URL được chỉ định, trả lại cho người gọi.

function loadBinaryResource(url) {
  const req = new XMLHttpRequest();
  req.open("GET", url, false);

  // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType("text/plain; charset=x-user-defined");
  req.send(null);
  return req.status === 200 ? req.responseText : "";
}

Phép thuật xảy ra trong dòng 5, ghi đè loại MIME, buộc trình duyệt phải coi nó là văn bản thuần túy, sử dụng bộ ký tự do người dùng định nghĩa. Điều này nói với trình duyệt không phân tích nó, và để cho các byte đi qua chưa được xử lý.

const filestream = loadBinaryResource(url);
const abyte = filestream.charCodeAt(x) & 0xff; // throw away high-order byte (f7)

Ví dụ trên tìm nạp byte tại Offset

function loadBinaryResource(url) {
  const req = new XMLHttpRequest();
  req.open("GET", url, false);

  // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType("text/plain; charset=x-user-defined");
  req.send(null);
  return req.status === 200 ? req.responseText : "";
}
3 trong dữ liệu nhị phân được tải. Phạm vi hợp lệ cho
function loadBinaryResource(url) {
  const req = new XMLHttpRequest();
  req.open("GET", url, false);

  // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType("text/plain; charset=x-user-defined");
  req.send(null);
  return req.status === 200 ? req.responseText : "";
}
3 là từ 0 đến
function loadBinaryResource(url) {
  const req = new XMLHttpRequest();
  req.open("GET", url, false);

  // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType("text/plain; charset=x-user-defined");
  req.send(null);
  return req.status === 200 ? req.responseText : "";
}
5.

Xem tải xuống các luồng nhị phân với xmlhttprequest để biết giải thích chi tiết. Xem thêm Tải xuống các tập tin.

Gửi dữ liệu nhị phân

Phương pháp

function loadBinaryResource(url) {
  const req = new XMLHttpRequest();
  req.open("GET", url, false);

  // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType("text/plain; charset=x-user-defined");
  req.send(null);
  return req.status === 200 ? req.responseText : "";
}
6 của XMLHTTPREQUEST đã được mở rộng để cho phép truyền dữ liệu nhị phân dễ dàng bằng cách chấp nhận đối tượng
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
4,
const req = new XMLHttpRequest();
req.open("GET", "/myfile.png", true);
req.responseType = "blob";

req.onload = (event) => {
  const blob = req.response;
  // ...
};

oReq.send();
5 hoặc
function loadBinaryResource(url) {
  const req = new XMLHttpRequest();
  req.open("GET", url, false);

  // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
  req.overrideMimeType("text/plain; charset=x-user-defined");
  req.send(null);
  return req.status === 200 ? req.responseText : "";
}
9.

Ví dụ sau đây tạo một tệp văn bản trên đường và sử dụng phương thức

const filestream = loadBinaryResource(url);
const abyte = filestream.charCodeAt(x) & 0xff; // throw away high-order byte (f7)
0 để gửi "tệp" đến máy chủ. Ví dụ này sử dụng văn bản thuần túy, nhưng bạn có thể tưởng tượng dữ liệu là một tệp nhị phân thay thế.

const req = new XMLHttpRequest();
req.open("POST", url, true);
req.onload = (event) => {
  // Uploaded
};

const blob = new Blob(["abc123"], { type: "text/plain" });

req.send(blob);

Gửi các mảng được đánh máy dưới dạng dữ liệu nhị phân

Bạn cũng có thể gửi các mảng được đánh máy JavaScript dưới dạng dữ liệu nhị phân.

// Create a new array with fake data (Consecutive numbers (0 - 255), looping back to 0)
const array = new Uint8Array(512).map((v, i) => i);

const xhr = new XMLHttpRequest();
xhr.open("POST", url, false);
xhr.send(array);

Đây là xây dựng một mảng 512 byte của số nguyên 8 bit và gửi nó; Tất nhiên, bạn có thể sử dụng bất kỳ dữ liệu nhị phân nào bạn muốn.

Gửi biểu mẫu và tải lên các tệp

Xin vui lòng, đọc đoạn này.

Kiểu dữ liệu nhị phân là gì?

Trong thống kê, dữ liệu nhị phân là một loại dữ liệu thống kê bao gồm dữ liệu phân loại có thể lấy chính xác hai giá trị có thể, chẳng hạn như "A" và "B" hoặc "đầu" và "đuôi".Nó cũng được gọi là dữ liệu nhị phân và một thuật ngữ cũ hơn là dữ liệu định lượng.Hai giá trị thường được gọi chung là "thành công" và "thất bại".a statistical data type consisting of categorical data that can take exactly two possible values, such as "A" and "B", or "heads" and "tails". It is also called dichotomous data, and an older term is quantal data. The two values are often referred to generically as "success" and "failure".

JavaScript có hỗ trợ dữ liệu nhị phân không?

Bạn cũng có thể gửi các mảng được đánh máy JavaScript dưới dạng dữ liệu nhị phân.Đây là xây dựng một mảng 512 byte của số nguyên 8 bit và gửi nó;Tất nhiên, bạn có thể sử dụng bất kỳ dữ liệu nhị phân nào bạn muốn.. This is building a 512-byte array of 8-bit integers and sending it; you can use any binary data you'd like, of course.

Tệp JS có phải là nhị phân không?

JavaScript không có loại "nhị phân" và vì vậy chúng đã đi với một chuỗi với bảo đảm rằng không có ký tự nào được lưu trữ trong chuỗi sẽ nằm ngoài phạm vi 0 .. and so they went with a String with the guarantee that no character stored in the string would be outside the range 0..

ArrayBuffer trong JavaScript là gì?

Đối tượng ArrayBuffer được sử dụng để biểu diễn bộ đệm dữ liệu nhị phân thô chung, có độ dài cố định.Nó là một mảng các byte, thường được gọi bằng các ngôn ngữ khác là "mảng byte".used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".