Skip to content

Consider adding a Bytes []byte method to File (or documenting an example of how it can be achieved). #54

@dmitshur

Description

@dmitshur

Currently, we have:

go-js-dom/dom.go

Lines 2460 to 2465 in 662b7b8

// File represents files as can be obtained from file choosers or drag
// and drop. The dom package does not define any methods on File nor
// does it provide access to the blob or a way to read it.
type File struct {
*js.Object
}

It might be helpful and worth considering changing it to have a Bytes() []byte method:

// File represents files as can be obtained from file choosers or drag
// and drop.
//
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/File.
type File struct { 
	*js.Object 
}

// Bytes reads the file f and returns its contents as a []byte.
func (f *File) Bytes() []byte {
	b := make(chan []byte)
	fr := js.Global.Get("FileReader").New()
	fr.Set("onload", func() {
		b <- js.Global.Get("Uint8Array").New(fr.Get("result")).Interface().([]byte)
	})
	fr.Call("readAsArrayBuffer", f.Object)
	return <-b
}

(Extracted from gopherjs/gopherjs#776 and #32. /cc @inkeliz)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions