我正在尝试阅读寓言中的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)
}
从视图中获取更多代码并更新上下文:
视图-处理拖放操作
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
谢谢
答案 0 :(得分:0)
我能够完成这项工作的唯一方法是使用本地存储。有点黑,但是可以用。
下面的代码供有兴趣的人使用。
查看:
...
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)
}