gpt4 book ai didi

f# - 如何使用拖放读取 Fable Elmish 中的 Browser.Blob

转载 作者:行者123 更新时间:2023-12-05 05:10:44 25 4
gpt4 key购买 nike

我正在尝试读取寓言 elmish 中的 Browser.Blob 对象 - 我可以在 javascript 中看到它是这样完成的,但我不确定如何处理 FileReader onload 事件。

var b = new Blob(...);
var fr = new FileReader();
fr.onload = function(evt) {
var res = evt.target.result;
console.log("onload",arguments, res, typeof res);
};
fr.readAsArrayBuffer(b);

我正在使用 ondrop 事件来读取已删除项目的文件列表。我可以获取文件名并获取 blob。下面的函数不起作用,因为我在 e.target.result 上收到错误,但我认为它无论如何都不会起作用,因为它会在设置之前返回 blobstr。

如何读取此函数中的 blob 字符串?

let encodeString (filename:string) (blob:Browser.Blob) () = promise {
try
let mutable blobstr = ""
Console.WriteLine(filename)
let fs = Browser.FileReader.Create()
fs.onload <- (fun e -> blobstr <- e.target.result)
fs.readAsBinaryString(blob)
return Result.Ok (filename,blobstr)
//return Result.Ok (filename,"")
with ex -> return (Error ex.Message)
}

来自 View 的更多代码和上下文更新:

View - 处理拖放:

        div [   Class "card border-primary"
Draggable true
Style [ Height "100px" ]
OnDragOver ( fun e ->
Console.WriteLine("hovering")
e.preventDefault()
NoAction |> dispatch
)
OnDrop ( fun e ->
Console.WriteLine("dropping")
e.preventDefault()
let fileList= e.dataTransfer.files
let files = [0. .. fileList.length - 1.]
|> List.map int
|> List.map (fun x -> fileList.item(float x))
let fileInfos = [
for file in files do
yield file.name, file.slice()
]
FilesLanded fileInfos |> dispatch
)
] []

更新:

    | FilesLanded files ->
model, Cmd.batch (
files
|> List.map ( fun (x,y) -> Cmd.ofPromise (encodeString x y ) () FileLanded UnexpectedError )
)
| FileLanded res ->
match res with
| Ok (name,contents) ->
Console.WriteLine(name)
Console.WriteLine(contents)
model, Cmd.none
| Error err ->
{ model with ErrorMessage= err }, Cmd.none
| UnexpectedError ex ->
{ model with ErrorMessage= string ex }, Cmd.none

谢谢

最佳答案

我能够实现此功能的唯一方法是使用本地存储。这有点 hacky,但它确实有效。

代码如下,感兴趣的可以引用。

查看:

...
div [ Class "card border-primary"
Draggable true
Style [ Height "100px" ]
OnDragOver ( fun e ->
Console.WriteLine("hovering")
e.preventDefault()
NoAction |> dispatch
)
OnDrop ( fun e ->
Console.WriteLine("dropping")
e.preventDefault()
let fileList= e.dataTransfer.files
let files = [0. .. fileList.length - 1.]
|> List.map int
|> List.map (fun x -> fileList.item(float x))
let fileInfos = [
for file in files do
yield file.name, file.slice()
]
FilesLanded fileInfos |> dispatch
)
] []

更新:

...
| FilesLanded files ->
model, Cmd.batch (
files
|> List.map ( fun (x,y) -> Cmd.ofPromise (encodeImageToUrl x y ) () FileLanded UnexpectedError )
)
| FileLanded res ->
Console.WriteLine(res)
match res with
| Ok img ->
let impurl = Browser.localStorage.getItem(sprintf "imp_%s" img.FileName).ToString()
printfn "successfully read file: %s - %s" img.FileName img.DisplayUrl
let imgWithUrl = { img with DisplayUrl= impurl }
{ model with ImportingImages= List.append model.ImportingImages [ imgWithUrl ] }, Cmd.none
| Error err ->
{ model with ErrorMessage= err }, Cmd.none

阅读图像:

    let encodeImageToUrl (filename:string) (blob:Browser.Blob) () = promise {
try
printf "%s: %f" filename blob.size
let fs = Browser.FileReader.Create()
fs.onload <- (fun e ->
printf "reader returned"
let res= e.target?result
Browser.localStorage.setItem(sprintf "imp_%s" filename, res)
)
fs.readAsDataURL(blob)
//the bloburl is empty at this point, we pull the actual url in the update method from local storage
return Result.Ok { FileName= filename; ImageBlob= blob; DisplayUrl= bloburl }
with ex -> return (Error ex.Message)
}

关于f# - 如何使用拖放读取 Fable Elmish 中的 Browser.Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56381466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com