Jonathan
Jonathan

Reputation: 11321

How can I read a local file in Elm?

I'm exploring the idea of replacing an XML->XSLT->HTML workflow with Elm, just to see if I can do it. I found an Elm XML parser, and now I just need to figure out how to read a local file into Elm. I can't seem to find anything anywhere that explains how to do that. How would I go about doing that?

Upvotes: 12

Views: 2971

Answers (1)

jacobm
jacobm

Reputation: 14025

You can't directly read a file in Elm. Depending on your needs, you have a few options:

  1. If your program only needs access to a static file, you can read in the file with Javascript and provide it to Elm as a flag (see here). This is the simplest way if it meets your needs.
  2. If you need to react to changes in the file somehow, you could again read the file in with Javascript but communicate using ports (see here).
  3. A possibly-simpler variation would be to stand up a web server that serves the file, and then interact with it in elm using HTTP requests (see here).

Upvotes: 18

Related Questions