2 回答

TA貢獻1831條經驗 獲得超4個贊
多年來我一直想要一個滿意的答案,前幾天晚上終于想通了。
您基本上可以將整個事情歸結為:
? ? fileInput := document.Call("getElementById", "fileInput")
? ? fileInput.Set("oninput", js.FuncOf(func(v js.Value, x []js.Value) any {
? ? ? ? fileInput.Get("files").Call("item", 0).Call("arrayBuffer").Call("then", js.FuncOf(func(v js.Value, x []js.Value) any {
? ? ? ? ? ? data := js.Global().Get("Uint8Array").New(x[0])
? ? ? ? ? ? dst := make([]byte, data.Get("length").Int())
? ? ? ? ? ? js.CopyBytesToGo(dst, data)
? ? ? ? ? ? // the data from the file is in dst - do what you want with it
? ? ? ? ? ??
? ? ? ? ? ? return nil
? ? ? ? }))
? ? ? ? return nil
? ? }))

TA貢獻1719條經驗 獲得超6個贊
您無法真正在瀏覽器中訪問文件系統。wasm_exec.js
用于在瀏覽器中執行 Go webassembly,它模擬了一些文件系統功能,但我認為它對你不是很有用,
文件讀取方法甚至默認返回錯誤。
你提到了<input type="file">
。您可以從上傳的文件中獲取字節:Getting byte array through input type = file。然后,您可以將這些字節傳遞給 Golang wasm 運行時。
在您的 Go 代碼中定義一個全局系統調用/js 回調,并從瀏覽器調用它以將字節向下傳遞到 Go 運行時。
我會尋找關于如何在 Go 運行時中定義回調的博文。還要注意 go 1.11 和 1.12 之間的變化,api 有重大變化。
- 2 回答
- 0 關注
- 345 瀏覽
添加回答
舉報