File Upload Handling #773
Replies: 5 comments 11 replies
-
I don't personally know but I'm sure someone has tried this. What have you tried so far? |
Beta Was this translation helpful? Give feedback.
-
The uploaded file has to go into a conversion process where it has to be converted to a string at JS end and has to be unmarshalled to a []byte at GO end to read the file. The conversion to string is required due to a strange behavior that I noticed while passing an array from JS to Go. All arrays passed from JS are being read as a map in GO. From my understanding this maybe because in JS, an array is basically a subset of the JS object and the counterpart of JS object in GO is a map. My workaround: JS: (file -> array buffer -> unit8array -> string) const file = e.target.files[0]
Promise.resolve(file.arrayBuffer())).then((ab: ArrayBuffer) => {
const u = new Uint8Array(ab);
SendFileFromJStoGo(`[${u.toString()}]`);
}); GO: (string -> []byte -> file) SendFileFromJStoGo(blob string) {
file := []byte{}
if err := json.Unmarshal([]byte(file), &zipFile); err != nil {
log.Fatal(err)
}
} For multiple file uploads: JS: (file -> array buffer -> unit8array -> string) const files = Array.from(e.target.files)
Promise.all(files.map((file) => file.file.arrayBuffer())).then((ab: ArrayBuffer[]) => {
const f = files.map((file) => {
const u = new Uint8Array(file);
return `[${u.toString()}]`;
});
SendFileFromJStoGo(f);
}); GO: (string -> []byte -> file) SendFileFromJStoGo(blobs []string) {
for _, file := range blobs {
file := []byte{}
if err := json.Unmarshal([]byte(file), &zipFile); err != nil {
log.Fatal(err)
}
}
} You may notice that for conversion of unit8array into string in JS, I have used template literals & |
Beta Was this translation helpful? Give feedback.
-
I'm also trying to figure out how to do this -- it's not an urgent blocking matter on my application, but it would be nice to support file drag-n-drop. I guess I'm not clear on how wails would turn a multipart HTTP form into a native function call. It'd be pretty cool if we could make a struct with a (I admit I haven't tried this yet, but I assume it's not currently implemented, based on this discussion.) |
Beta Was this translation helpful? Give feedback.
-
The Optimus project has DnD. There's nteresting discussions on this topic here: microsoft/vscode#115807 |
Beta Was this translation helpful? Give feedback.
-
ContextI needed to upload TLS files in a Wails + TS React software. SolutionI did the trick inspired by the @slo-loris solution: const file: File = event.target.files[0];
file.arrayBuffer().then((fileBuffer: ArrayBuffer) => {
const decoder: TextDecoder = new TextDecoder("utf-8");
return decoder.decode(fileBuffer);
}).then((filecontent: string) => {
// Do what you want with the decoded file in a string
}); Explanations
Best Practices & Security
|
Beta Was this translation helpful? Give feedback.
-
I have to create a CRM portal and I want to handle file upload via html
input[type=file]
so it is possible to pass a file to wails backend binded function.If yes then how?
Beta Was this translation helpful? Give feedback.
All reactions