我对 Elm 还比较陌生,目前遇到了一个关于从后端获取数据填充模型的问题。我可以向服务器发起 GET 请求并返回一个字节数组(数据可能是任何类型的图像、音频或视频),当使用 Html.img 显示这些数据时,一切都运行良好。但是,当我尝试使用 Http.get (src:https://package.elm-lang.org/packages/elm/http/latest/Http) 来填充我的模型时,它需要解码器。问题在于 Bytes.Decode.bytes 需要一个整数来知道需要解码多少个字节。
所以我的问题是:有没有办法在仍然匹配 Http.get 的类型模式的情况下访问字节宽度?
以下是我的问题的简单示例:
import Bytes exposing (Bytes)
import Bytes.Decode exposing (Decoder, bytes, decode)
import GeneralTypes exposing (Msg(..))
import Http
getMediaFromUrl : Cmd Msg
getMediaFromUrl = Http.get
{ url = "http://localhost:8090/image/2006/aa@a.de/session"
, expect = Http.expectBytes GetThumbnail decodeBytes
}
decodeBytes: Bytes.Bytes -> Decoder Bytes
decodeBytes bytesToDecode= let
fileSize =
bytesToDecode |> Bytes.width
in
Bytes.Decode.bytes fileSize
module GeneralTypes exposing (..)
import Bytes exposing (Bytes)
import Http
type Msg = GetThumbnail (Result Http.Error Bytes)