0

loading locally stored binary files

Hi

I'm trying to load a binary file from my local Resources directory but keep getting an XMLHttpRequest.status return value of 0.

Am I running into some security issues or just doing it completely wrong ?

Thanks
Marc

CODE
var request = new XMLHttpRequest();
request.open("GET", "Resources/data.bin", false);
request.setRequestHeader( "Content-type", "text/plain; charset=x-user-defined" );
request.send(null);

by
11 Replies
  • This is just an idea, but I believe the Widget platform uses SpiderMonkey as the Javascript engine. If this is the case, you may need to do something similar to what you would do in Firefox, and use the overrideMimeType method, if it even exists. It is not documented in the Yahoo Developer Guide though.

    CODE
    function load_binary_resource(url) {
    var req = new XMLHttpRequest();
    req.open('GET', url, false);
    //XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
    req.overrideMimeType('text/plain; charset=x-user-defined');
    req.send(null);
    if (req.status != 200) return '';
    return req.responseText;
    }


    See this link:
    https://developer.mozilla.org/En/Using_XMLH...ing_binary_data
    0
  • QUOTE (marc.coupar @ Oct 1 2009, 04:05 AM) <{POST_SNAPBACK}>
    Hi

    I'm trying to load a binary file from my local Resources directory but keep getting an XMLHttpRequest.status return value of 0.

    Am I running into some security issues or just doing it completely wrong ?

    Thanks
    Marc

    CODE
    var request = new XMLHttpRequest();
    request.open("GET", "Resources/data.bin", false);
    request.setRequestHeader( "Content-type", "text/plain; charset=x-user-defined" );
    request.send(null);



    Hello Marc,

    Several issues here:

    1.) XHR does not allow you to fetch resources from anything but a webserver. Please notice the "HTTP Request" in the naming.

    2.) You are not allowed to read data from the filesystem in this manor.

    -Jeremy
    0
  • QUOTE (Jeremy Johnstone @ Oct 1 2009, 01:30 PM) <{POST_SNAPBACK}>
    Hello Marc,

    Several issues here:

    1.) XHR does not allow you to fetch resources from anything but a webserver. Please notice the "HTTP Request" in the naming.

    2.) You are not allowed to read data from the filesystem in this manor.

    -Jeremy


    Hi Jeremy

    I'm porting a couple of interactive TV games (written in C) in order to familiarise myself with the platform so perhaps some of my thinking is still orientated in the old platform. Can you recommend best practice for the Widget platform eg convert my data files to text and access them locally or keep them as binary and store on a server for access that way?

    I know that's a bit of a vague question with different factors affecting the decision but if you could give me a couple of examples of existing applications where you've opted for a specific approach and why, that should be enough to help me determine the best option in each case.

    Thanks
    Marc
    0
  • Marc,
    You cannot run anything but Javascript in your widget. It is not suggested to make many writes through the limited file system as there is limited write cycles on the flash-based storage. There are a few widgets that are included in the WDK that demonstrate the various functionality of the widget framework. You can can find the code for these in /usr/local/Konfabulator/TV/Widgets/.

    Hope that helps,
    Jim
    0
  • QUOTE (Jim @ Oct 1 2009, 01:57 PM) <{POST_SNAPBACK}>
    Marc,
    You cannot run anything but Javascript in your widget. It is not suggested to make many writes through the limited file system as there is limited write cycles on the flash-based storage. There are a few widgets that are included in the WDK that demonstrate the various functionality of the widget framework. You can can find the code for these in /usr/local/Konfabulator/TV/Widgets/.

    Hope that helps,
    Jim


    Hi Jim

    I might have explained it badly. I'm not trying to execute C code from the Widget!! I've been converting the original code and algorithms to javascript and, running in the simulator at least, all is going to plan and I've nearly completed the first game. However, I'm trying to keep code algorithms as similar as possible to the original C so if I can keep my data files as binary then I can simply port some of the C code rather than change both the data itself and the data extraction techniques.

    Cheers
    Marc
    0
  • I think the best solution would be to store everything on a server in the cloud. The Flash-Drive on the TV should be used as less as possible, because the Flash-Drives have very limited write counts. If you write too often, the drive will "burn out" :) .
    0
  • I'm pretty sure (from other threads) you're not going to be able to locally store binary files and read them in your widget. You can't do even text files. The only files you can really locally store and access are .js files. They need to be Javascript because they will be eval'd (or some equivalent) on load.

    I think you're going to need to port the data files.
    0
  • I have a feeling you're going to have to use the set() and get() methods from currentAppData and currentAppConfig, although I am unaware if they can store binary files or not. I've only used them to store It might be easier (as stated in this thread) to store the files on a webserver and pull them from there. It might be a hit in bandwidth, but there's definitely supposed to be minimized writing on the flash storage.

    It might be ugly but you could set a variable in currentAppData/Config that contained all the binary as a string, I'm pretty sure there's no limit to string size in javascript..
    0
  • There is definitely a limit to what your widget is allowed to store on the flash, whether in the widget itself or in currentAppData. How big is this binary file you are parsing? I have a feeling performance of parsing will be abysmally slow unless it's a tiny file and if it's a tiny file, then what's the point?

    -Jeremy
    0
  • QUOTE (Jeremy Johnstone @ Oct 2 2009, 07:12 PM) <{POST_SNAPBACK}>
    There is definitely a limit to what your widget is allowed to store on the flash, whether in the widget itself or in currentAppData. How big is this binary file you are parsing? I have a feeling performance of parsing will be abysmally slow unless it's a tiny file and if it's a tiny file, then what's the point?

    -Jeremy


    As a side question...Is there a specified limit to the amount of data we can write to currentAppData/flash? We intended to store somewhere around 1K worth of data, but will not be performing many writes once the data is initially written.
    0
  • QUOTE (Steve @ Oct 5 2009, 03:03 AM) <{POST_SNAPBACK}>
    As a side question...Is there a specified limit to the amount of data we can write to currentAppData/flash? We intended to store somewhere around 1K worth of data, but will not be performing many writes once the data is initially written.



    1Kb shouldn't be a problem, especially if it's infrequently written to.

    -Jeremy
    0

Recent Posts

in General - Yahoo! TV Widgets