# Saving data to local files in JavaScript

**URL:** <https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229>\
**Category:** How-to Discussions\
**Created:** [August 15, 2015, 8:22am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229 "2015-08-15T08:22:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 15, 2015, 8:22am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/1 "2015-08-15T08:22:39Z")

</div>

Is there any method currently available which will allow me write to/create local files and subsequently read from them, particularly in JS?

---

<div class="post-metadata">

**Author:** ![Anders\_Lassen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_lassen/32/522_2.png) [@Anders\_Lassen](https://forums.fusetools.com/u/Anders_Lassen)\
**Post date:** [August 16, 2015, 6:09pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/2 "2015-08-16T18:09:31Z")

</div>

Hi!

In version 0.5.3373 there are no JS APIs for this, but it’s coming very soon 🙂

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 17, 2015, 12:39am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/3 "2015-08-17T00:39:27Z")

</div>

Awesome, thanks!

EDIT: In the meantime, I tried to make a JavaScript module in Uno that would read/write files but I’m running into some trouble. Here’s what I have so far:

FileOps.uno:

```auto
using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.IO;

public class FileOps : Fuse.Scripting.NativeModule
{
    public FileOps()
    {
        AddMember(new NativeFunction("FileWrite", (NativeCallback)FileWrite));
        AddMember(new NativeFunction("FileRead", (NativeCallback)FileRead));
    }

    static object FileWrite(Context c, object[] args)
    {
        string filepath = args[0];
        string text = args[1];

        Uno.IO.File.WriteAllText(filepath, text);

        return null;
    }

    static object FileRead(Context c, object[] args)
    {
        string filepath = args[0];
        string text = Uno.IO.File.ReadAllText(filepath);

        return text;
    }
}

```

Then in the MainView.ux file:

```auto
<FileOps ux:Global='FileOps' />
<JavaScript>
  var FileWrite = require("FileOps").FileWrite;
  var FileRead = require("FileOps").FileRead;

  FileWrite("file.txt", "testing writer")
</JavaScript>

```

But I get this error:

```auto
D:\My Documents\PC Accessories\Programming\Javascript\Fuse Projects\TimetableUpdater\FileOps.uno(8,24): E3111: Fuse.Scripting does not contain type or namespace 'NativeModule'. Could you be missing a package reference?
D:\My Documents\PC Accessories\Programming\Javascript\Fuse Projects\TimetableUpdater\FileOps.uno(16,26): E3114: There is no identifier named 'Context' accessible in this scope. 
D:\My Documents\PC Accessories\Programming\Javascript\Fuse Projects\TimetableUpdater\FileOps.uno(26,25): E3114: There is no identifier named 'Context' accessible in this scope. 

```

I’m following the instructions here: [https://www.fusetools.com/developers/guides/fusejs/nativemodules](https://www.fusetools.com/developers/guides/fusejs/nativemodules)

---

<div class="post-metadata">

**Author:** ![Anders\_Lassen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_lassen/32/522_2.png) [@Anders\_Lassen](https://forums.fusetools.com/u/Anders_Lassen)\
**Post date:** [August 17, 2015, 12:47pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/4 "2015-08-17T12:47:23Z")

</div>

Hi!

Make sure you add a reference to `Fuse.Scripting` and `Fuse.Reactive` in your project.

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 17, 2015, 1:57pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/5 "2015-08-17T13:57:56Z")

</div>

Hi there! Do you mean in the .uno or in the .ux? Because I think I did in the Uno file, is this right?

```auto
using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.IO;

```

If you meant .ux, how would I go about doing that? Thanks!

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 17, 2015, 2:02pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/6 "2015-08-17T14:02:12Z")

</div>

Hi there! Do you mean in the .uno or in the .ux? Because I think I did in the Uno file, is this right?

```auto
using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.IO;

```

If you meant .ux, how would I go about doing that? Thanks!

---

<div class="post-metadata">

**Author:** ![Anders\_Lassen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_lassen/32/522_2.png) [@Anders\_Lassen](https://forums.fusetools.com/u/Anders_Lassen)\
**Post date:** [August 17, 2015, 2:26pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/7 "2015-08-17T14:26:02Z")

</div>

In your `.unoproj`

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 17, 2015, 3:48pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/8 "2015-08-17T15:48:40Z")

</div>

Ah I see, that fixed it. Although now I’m getting:

```auto
D:\My Documents\PC Accessories\Programming\Javascript\Fuse Projects\TimetableUpdater\FileOps.uno(12,45): E2029: '<method_group>' has no defined cast to type Fuse.Scripting.NativeCallback
D:\My Documents\PC Accessories\Programming\Javascript\Fuse Projects\TimetableUpdater\FileOps.uno(13,44): E2029: '<method_group>' has no defined cast to type Fuse.Scripting.NativeCallback

```

FileOps.uno:

```auto
using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.IO;

public class FileOps : NativeModule
{
    public FileOps()
    {
        AddMember(new NativeFunction("FileWrite", (NativeCallback)FileWrite));
        AddMember(new NativeFunction("FileRead", (NativeCallback)FileRead));
    }

    static object FileWrite(Context c, string[] args)
    {
        string filepath = args[0];
        string text = args[1];

        Uno.IO.File.WriteAllText(filepath, text);

        return null;
    }

    static object FileRead(Context c, string[] args)
    {
        string filepath = args[0];
        string text = Uno.IO.File.ReadAllText(filepath);

        return text;
    }
}

```

---

<div class="post-metadata">

**Author:** ![Anders\_Lassen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_lassen/32/522_2.png) [@Anders\_Lassen](https://forums.fusetools.com/u/Anders_Lassen)\
**Post date:** [August 17, 2015, 5:04pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/9 "2015-08-17T17:04:33Z")

</div>

Hi!

The arguments must be of type `object`.

```auto
static object FileRead(Context c, object[] args)
{
    string filepath = args[0] as string;

    if (filepath == null) throw new Error("FileRead(): argument must be string");

    string text = Uno.IO.File.ReadAllText(filepath);

    return text;
}

```

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 18, 2015, 2:46am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/10 "2015-08-18T02:46:45Z")

</div>

If I’m understanding right, it’s saying NativeModule is sealed?

```auto
D:\My Documents\PC Accessories\Programming\Javascript\Fuse Projects\TimetableUpdater\FileOps.uno(8,8): E4009: Cannot use a sealed class as base class

```

FileOps.uno:

```auto
using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.IO;

public class FileOps : NativeModule
{
    public FileOps()
    {
        AddMember(new NativeFunction("FileWrite", (NativeCallback)FileWrite));
        AddMember(new NativeFunction("FileRead", (NativeCallback)FileRead));
    }

    static object FileWrite(Context c, object[] args)
    {
        if (args.Length != 2) throw new Error("FileWrite(): takes 2 arguments, " + args.Length.ToString() + " provided");

        string filepath = args[0] as string;
        string text = args[1] as string;

        if (filepath == null) throw new Error("FileWrite(): filepath argument must be string");
        if (text == null) throw new Error("FileWrite(): text argument must be string");

        Uno.IO.File.WriteAllText(filepath, text);

        return null;
    }

    static object FileRead(Context c, object[] args)
    {
        if (args.Length != 1) throw new Error("FileRead(): takes 1 argument, " + args.Length.ToString() + " provided");

        string filepath = args[0] as string;

        if (filepath == null) throw new Error("FileRead(): argument must be string");

        string text = Uno.IO.File.ReadAllText(filepath);

        return text;
    }
}

```

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 18, 2015, 2:48am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/11 "2015-08-18T02:48:13Z")

</div>

EDIT: Oops, didn’t mean to double-post!

---

<div class="post-metadata">

**Author:** ![Anders\_Lassen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_lassen/32/522_2.png) [@Anders\_Lassen](https://forums.fusetools.com/u/Anders_Lassen)\
**Post date:** [August 18, 2015, 8:57am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/12 "2015-08-18T08:57:21Z")

</div>

Hi,

Thanks for spotting this - this is actually a bug. `NativeModule` was accidentally made `sealed` in the previous release (v0.5.3373), breaking that example.

I have fixed this now, so the next release (Monday) will include that fix + a local storage API for JS 🙂

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 19, 2015, 5:14am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/13 "2015-08-19T05:14:11Z")

</div>

That sounds awesome, thanks again Anders!

---

<div class="post-metadata">

**Author:** ![martinmuda](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/martinmuda/32/179_2.png) [@martinmuda](https://forums.fusetools.com/u/martinmuda)\
**Post date:** [August 23, 2015, 2:13am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/14 "2015-08-23T02:13:45Z")

</div>

Hi Anders, besides local storage API for JS are you considering use database engines such as mongodb or similars? Thank you

---

<div class="post-metadata">

**Author:** ![cio](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/cio/32/518_2.png) [@cio](https://forums.fusetools.com/u/cio)\
**Post date:** [August 24, 2015, 5:44am UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/15 "2015-08-24T05:44:57Z")

</div>

Hi Anders, we need to use NativeModule ASAP. When is fixed release coming out?

---

<div class="post-metadata">

**Author:** ![HeyItsJono](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/heyitsjono/32/60_2.png) [@HeyItsJono](https://forums.fusetools.com/u/HeyItsJono)\
**Post date:** [August 24, 2015, 4:31pm UTC](https://forums.fusetools.com/t/saving-data-to-local-files-in-javascript/1229/16 "2015-08-24T16:31:59Z")

</div>

Cio, I can confirm it’s working perfectly on my end on Fuse v0.5.3524!
