WebOS : how to test if a file exists?
Is it possible to test if a file exists in WebOS before proceeding with a download using the download man开发者_运维百科ager service?
By default the method "download" will add an underscore + number to avoid overwriting, but what i'm trying to do is to avoid downloading a file if it already exists on the local /media/internal/files folder...
I looked all over the web but to no avail... Any clue? Thanks!
Probably the most straightforward way is to use an Ajax request to try to read the file. http://forums.precentral.net/web-os-development/196320-how-parse-text-file-pres-file-system-into-app.html
If you can't read the file, it obviously hasn't been downloaded yet.
Edited to add the following from the webOS 1.4 release notes:
For Ajax.Request, if the file being requested does not exist, the request no longer gets a "404", which is considered a failure, but a "0", which is considered a success. You will need to modify your code to look for a transport.status of 0 for a file that does not exist, or 200 for a file that does. See the Mozilla documentation for more information. Example:
new Ajax.Request('/media/internal/my.file', {
method: 'get',
onSuccess: function(transport) {
//This is new for 1.4, we have to check the status of
//the transport object to see if the file exists or not.
if (transport.status == 200)
Mojo.Log.info('200 = http ok, file exists')
else if (transport.status == 0)
Mojo.Log.info('0 = response was empty, file does not exist')
},
onFailure: function(transport) {
Mojo.Log.info('In 1.3.5 if the file didn’t exist the request would return here')
}
});
You can also create a private service that leverages NodeJS.
http://nodejs.org/docs/v0.3.1/api/fs.html#fs.statSync
精彩评论