# Easier Sending a File Through a Port (or double-decoding a Json value)

**URL:** <https://discourse.elm-lang.org/t/easier-sending-a-file-through-a-port-or-double-decoding-a-json-value/6498>\
**Category:** Show and Tell\
**Created:** [November 8, 2020, 9:10pm UTC](https://discourse.elm-lang.org/t/easier-sending-a-file-through-a-port-or-double-decoding-a-json-value/6498 "2020-11-08T21:10:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![curiousme](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@curiousme](https://discourse.elm-lang.org/u/curiousme)\
**Post date:** [November 8, 2020, 9:10pm UTC](https://discourse.elm-lang.org/t/easier-sending-a-file-through-a-port-or-double-decoding-a-json-value/6498/1 "2020-11-08T21:10:03Z")

</div>

While working on chunked file upload (that needs slicing the file on Javascript side) I have to keep around both a raw `Value` of dropped files (to send through a port), and a `File` value (to render the UI).

In thinking how to do that with some elegance, it occurred to me that `Json.Decode.mapN` actually applies decoders in a loop on the same object, allowing one to produce multiple representations of the object being decoded:

```
fileWithRaw : Decoder (File, Value)
fileWithRaw =
    Json.Decode.map2 Tuple.pair
        File.decoder
        Json.Decode.value

```

Never thought of it before; somehow one defaults to thinking about `decode` being similar to `parse` that consumes the tokens it parses and creates new values. Maybe `Json.` in the module name makes one think of that. Perhaps it’s a mild misnomer: the module has nothing to do with neither the notation, nor decoding in the sense of processing the contents of objects. No strings are involved, and decoders only wrap references to JS objects with Elm values.

[Here’s](https://ellie-app.com/btPd2KRPTM3a1) the updated Ellie from [a previous discussion](https://discourse.elm-lang.org/t/sending-a-file-through-a-port/3440/11) using this technique.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 18, 2020, 9:10pm UTC](https://discourse.elm-lang.org/t/easier-sending-a-file-through-a-port-or-double-decoding-a-json-value/6498/2 "2020-11-18T21:10:06Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
