You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Cordova弃用file-transfer插件及XHR2示例的技术疑问

Hey there, let's tackle your questions about Cordova's move away from cordova-plugin-file-transfer and that official sample code—super valid points you're raising!

1. What's the purpose of creating fileEntry if we don't write the Blob to it?

Great catch! That official sample does feel a bit confusing because it sets up a fileEntry via fs.root.getFile() but never actually writes the downloaded Blob to it. The likely reason here is that the example is trying to combine two separate concepts in one snippet:

  • How to access the Cordova file system (to get a fileEntry reference for future use)
  • How to use XHR2 to fetch binary data as a Blob

It's probably meant as a starting point—showing you how to get a file entry ready, so you could extend the code later to write the Blob to that file if needed. As written, though, the fileEntry part is redundant for the actual functionality demonstrated (displaying the image via createObjectURL or reading it with FileReader).

2. Is this sample only demonstrating Blob fetching, not file system writing?

Exactly! The example's core goal is to show you how to replace file-transfer with standard XHR2 for fetching binary data. It doesn't include writing to the file system because:

  • Adding FileWriter logic would complicate the sample's focus
  • The official docs explicitly note that FileWriter has memory limitations for large files (since it works with Blobs loaded entirely into memory)

If you wanted to extend the sample to write the Blob to the fileEntry, you'd add code like this inside the oReq.onload callback:

fileEntry.createWriter(function(fileWriter) {
  fileWriter.write(blob);
  fileWriter.onwriteend = function() {
    console.log("Blob written to file successfully");
  };
  fileWriter.onerror = function(e) {
    console.error("Error writing file: ", e);
  };
});

But as you noted, this approach isn't ideal for files over 5MB due to memory constraints.

3. Can we still handle large (hundreds of MB) file downloads without cordova-plugin-file-transfer?

Absolutely—you don't need the deprecated plugin anymore! The key is to use streaming or chunked writing instead of loading the entire file into memory as a Blob. Here are two reliable approaches:

  • XHR with progress events + chunked FileWriter: Skip setting responseType: "blob", and instead listen to the XHR's progress event to write data chunks to the file system as they arrive. This avoids holding the entire file in memory.
  • Fetch API with ReadableStream: Modern Cordova web views support the Fetch API and ReadableStreams. You can stream the response body and write each small chunk to the file incrementally, which is far more efficient for large files.

Both methods let you handle hundreds of MB files without hitting memory limits, so you can fully replace cordova-plugin-file-transfer for all file operations.


内容的提问来源于stack exchange,提问作者geoM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:42:24