Cordova iOS+Meteor中如何通过本地路径创建File对象[JavaScript]
Hey Igor, I’ve been in your exact situation when building a Meteor + Cordova iOS app with a custom camera plugin—getting a valid File object from a local video path isn’t straightforward because Cordova’s local file system doesn’t play nice with the browser’s native File constructor. Here’s how I fixed it:
Key Background
Cordova uses its own file system URLs (like cdvfile:// or file://), which the standard browser File API can’t directly parse. You need to use Cordova’s cordova-plugin-file to access and convert the local file into a proper File object.
Step 1: Install the Required Cordova Plugin
First, make sure you have the cordova-plugin-file installed in your Meteor project. Run this command in your project root:
meteor add cordova:cordova-plugin-file@6.0.2
(You can use the latest version if preferred—this is a stable, widely-used release.)
Step 2: Resolve the Local Path to Get a File Object
Use Cordova’s window.resolveLocalFileSystemURL to parse the local video path, retrieve a FileEntry, then extract the File object from it. Here’s a complete code example:
// Replace with your actual video local path from the camera plugin const videoLocalPath = "cdvfile://localhost/temporary/my-recorded-video.mp4"; if (Meteor.isCordova) { window.resolveLocalFileSystemURL( videoLocalPath, (fileEntry) => { // Get the File object from the FileEntry fileEntry.file( (file) => { // Success! `file` is a valid standard File object console.log("Successfully retrieved File object:", file); // Use this File object for uploads, processing, etc. }, (fileError) => { console.error("Failed to get File from FileEntry:", fileError); } ); }, (resolveError) => { console.error("Failed to resolve local file path:", resolveError); } ); }
Why Your Previous Attempts Didn’t Work
- Directly using
new File('etc/path')fails because the browser’sFileconstructor expects binary data, not a file path. - Creating a
Blobfirst doesn’t work because you weren’t actually reading the video file’s content from the local file system—you were just creating an empty or invalid Blob without the actual video data.
Bonus: Handling Edge Cases
- If your camera plugin returns a
file://path instead ofcdvfile://, the same code will still work—resolveLocalFileSystemURLsupports both formats. - Always wrap this code in
Meteor.isCordovato ensure it only runs on the Cordova device (not in the web browser).
内容的提问来源于stack exchange,提问作者Igor Djukic

