FileSystemFileEntry: createWriter() method

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.

Phương thức createWriter() của giao diện FileSystemFileEntry trả về đối tượng FileWriter có thể được sử dụng để ghi dữ liệu vào tệp được đại diện bởi mục thư mục.

Cú pháp

js
createWriter(successCallback)
createWriter(successCallback, errorCallback)

Tham số

successCallback

Hàm callback được gọi khi FileWriter được tạo thành công; FileWriter được truyền vào callback là tham số duy nhất.

errorCallback Optional

Nếu được cung cấp, đây phải là phương thức được gọi khi xảy ra lỗi trong khi cố gắng tạo FileWriter. Callback này nhận đối tượng DOMException mô tả lỗi làm đầu vào.

Giá trị trả về

None (undefined).

Ví dụ

Ví dụ này thiết lập phương thức writeToFileEntry() xuất chuỗi văn bản vào tệp tương ứng với mục thư mục được truyền vào.

js
function writeToFileEntry(entry, text) {
  entry.createWriter(
    (fileWriter) => {
      let data = Blob([text], { type: "text/plain" });

      fileWriter.write(data);
    },
    (error) => {
      /* do whatever to handle the error */
    },
  );
}

Callback thành công cho lệnh gọi createWriter() lấy văn bản được truyền vào và tạo đối tượng Blob mới kiểu text/plain chứa văn bản đó. Blob này sau đó được xuất vào đối tượng FileWriter để ghi vào tệp.

Thông số kỹ thuật

Tính năng này không còn là một phần của bất kỳ đặc tả nào nữa. Nó không còn trên lộ trình để trở thành tiêu chuẩn.

Tương thích trình duyệt

Xem thêm