admin管理员组文章数量:1405636
Specifically, how to connect <input type="file">
with this function in Go?
I know there is "syscall/js" package, but I didn't find any examples with file reading.
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, ma))
}
return labelsWithFeatures
}
Specifically, how to connect <input type="file">
with this function in Go?
I know there is "syscall/js" package, but I didn't find any examples with file reading.
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, ma))
}
return labelsWithFeatures
}
Share
Improve this question
asked Jan 20, 2019 at 22:29
stsdcstsdc
4466 silver badges16 bronze badges
4
- You can't interact with the local filesystem within a browser for security reasons. – Adrian Commented Jan 21, 2019 at 14:24
-
Maybe you need to call
FileReader
JavaScript API ( developer.mozilla/en-US/docs/Web/API/File/… ) usingsyscall/js
blog.gopheracademy./advent-2018/go-in-the-browser – zakki Commented Jan 22, 2019 at 8:21 - @Adrian You sure can, you just have to ask the user to give you the file. There's APIs as well as just an upload input. – donatJ Commented Aug 8, 2020 at 0:59
- @donatJ that's not the same thing as interacting with the local filesystem. You can't open an arbitrary file path from JavaScript or examine a directory. The only way to access a file is if the user provides it to a file form field. – Adrian Commented Aug 10, 2020 at 12:35
2 Answers
Reset to default 6I've wanted a satisfactory answer for this for years, finally figured it out the other night.
You can essentially boil the whole thing down to:
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
}))
I wrote a little blog post about it here with the working WASM code running at the bottom
https://donatstudios./Read-User-Files-With-Go-WASM
You can't really access the filesystem in the browser. wasm_exec.js
is used to execute Go webassembly in the browser, it mocks out some filesystem functionality, but I don't think it's very useful to you: https://github./golang/go/blob/9d23975d/misc/wasm/wasm_exec.js#L41-L73
The file read method even returns an error by default.
You mentioned <input type="file">
. You can get bytes from an uploaded file: Getting byte array through input type = file. You could then pass those bytes to the Golang wasm runtime.
Define a global syscall/js callback in your Go code and call it from the browser to pass the bytes down to the Go runtime.
I would look for blogposts on how to define callbacks from within the Go runtime. Also look out for changes between go 1.11 and 1.12, the api has breaking changes.
本文标签: javascriptHow to read file from disk and pass it to WebAssembly using GoStack Overflow
版权声明:本文标题:javascript - How to read file from disk and pass it to WebAssembly using Go? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744279708a2598591.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论