如何在Visual Studio 2017中使cordova-plugin-file正常工作?
Hey there! Let's work through fixing your cordova-plugin-file issues step by step—since it sounds like the plugin isn't initializing properly, and we can also tackle the TypeScript hinting problem along the way.
First: Fix the Core Plugin Initialization Issue
The biggest red flag here is that cordova.file.applicationDirectory is returning null. This almost always happens because you're calling Cordova APIs before the deviceready event fires—Cordova doesn't fully initialize its APIs until this event triggers.
Here's how to adjust your code to wait for the device to be ready:
// Wait for Cordova to fully initialize before accessing APIs document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { // Now it's safe to call Cordova functions findCampaignFolders(); } function listDir(path) { alert("in Listing Func with path: " + path); window.resolveLocalFileSystemURL(path, function (fileSystem) { var reader = fileSystem.createReader(); reader.readEntries( function (entries) { alert("the right functions"); var textString = ""; for (var i = 0; i < entries.length; i++) { // Add .name to show actual file/folder names instead of [object Object] textString += entries[i].name + "\n"; } alert(textString); }, function (err) { console.log("Error reading entries: ", err); } ); }, function (err) { console.log("Error resolving path: ", err); }); } function findCampaignFolders() { // Use JSON.stringify to see the full cordova.file object instead of [Object Object] alert("in find folder cordova: " + JSON.stringify(cordova.file)); var targetPath = cordova.file.applicationDirectory + "www"; alert("Target path: " + targetPath); listDir(targetPath); alert("Did you see Files"); }
Second: Fix Visual Studio 2017 Code Hints & Type Issues
The empty CordovaPlugins interface in index.d.ts means TypeScript doesn't recognize the file plugin's types. Here's how to fix that:
- Install the official TypeScript type definitions for Cordova and the file plugin:
npm install @types/cordova @types/cordova-plugin-file --save-dev - Update your
index.d.tsto reference the file plugin types:/// <reference types="cordova-plugin-file" /> interface CordovaPlugins { // The type definitions will automatically populate this interface now }
This should give you proper code completion and let you navigate to symbol definitions.
Third: Verify Plugin Installation
If the above doesn't work, double-check the plugin is properly installed:
- Run
cordova plugin listin your project root—you should seecordova-plugin-file@4.3.3listed. - If not, reinstall the plugin:
cordova plugin remove cordova-plugin-file cordova plugin add cordova-plugin-file@4.3.3 - Make sure you're testing on a real device or emulator (not just the browser)—some
cordova.fileproperties don't work in browser mode.
Alternative File System Options
If you're still struggling with Cordova's file plugin, consider these alternatives:
- Capacitor Filesystem API: A modern, TypeScript-first replacement for Cordova plugins. It has simpler configuration and better cross-platform support. Example code to list files:
import { Filesystem, Directory, ReaddirResult } from '@capacitor/filesystem'; async function listWwwFiles() { try { const result: ReaddirResult = await Filesystem.readdir({ path: '', directory: Directory.Application }); alert('Files: ' + result.files.map(f => f.name).join('\n')); } catch (err) { console.error('Error:', err); } } - Direct HTTP Requests: If you only need to read static files from the
wwwfolder, you can usefetch('path/to/your/file')directly—no plugin needed. This won't work for writing files, though.
内容的提问来源于stack exchange,提问作者Kavery

