sts*_*sdc 4 javascript go webassembly
<input type="file">具体来说, Go中如何连接这个函数呢?我知道有“syscall/js”包,但我没有找到任何文件读取的示例。
func parseCSVFile(filePath string) []LabelWithFeatures {
fileContent, _ := ioutil.ReadFile(filePath)
lines := bytes.Split(fileContent, newline)
numRows := len(lines)
labelsWithFeatures := make([]LabelWithFeatures, numRows-2)
for i, line := range lines {
// skip headers
if i == 0 || i == numRows-1 {
continue
}
labelsWithFeatures[i-1] = NewLabelWithFeatures(bytes.Split(line, comma))
}
return labelsWithFeatures
}
Run Code Online (Sandbox Code Playgroud)
多年来我一直想要一个满意的答案,那天晚上终于找到了。
您基本上可以将整个事情归结为:
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
}))
Run Code Online (Sandbox Code Playgroud)
我在这里写了一篇关于它的小博文,其中运行的 WASM 代码在底部运行
https://donatstudios.com/Read-User-Files-With-Go-WASM
| 归档时间: |
|
| 查看次数: |
3961 次 |
| 最近记录: |