Wasm-bindgen: Converting a File or Blob into a byte slice &[u8]

Created on 24 Feb 2019  路  2Comments  路  Source: rustwasm/wasm-bindgen

I'm tryin to implement drag and drop and I'd like to go get the byte slice for the file that is uploaded.

I can easily get a File or Blob, but I'm unsure about how to convert them into a byte slice.

Is there a way to do that right now?

Thanks!!

Most helpful comment

Just in case anyone else comes here because they're searching about file uploads....

My use case ended up looking like this:

html! {
<div
    ondrop=move |event: web_sys::DragEvent| {
       event.prevent_default();
       event.stop_propagation();

       let store = Rc::clone(&store_clone);

       let dt = event.data_transfer().unwrap();
       let files = dt.files().unwrap();
       let psd = files.item(0).unwrap();

       let file_reader = web_sys::FileReader::new().unwrap();
       file_reader.read_as_array_buffer(&psd).unwrap();

       let mut onload = Closure::wrap(Box::new(move |event: Event| {
           let file_reader: FileReader = event.target().unwrap().dyn_into().unwrap();
           let psd = file_reader.result().unwrap();
           let psd = js_sys::Uint8Array::new(&psd);

           let mut psd_file = vec![0; psd.length() as usize];
           psd.copy_to(&mut psd_file);

           store.borrow_mut().msg(&Msg::ReplacePsd(&psd_file));
       }) as Box<FnMut(_)>);

       file_reader.set_onload(Some(onload.as_ref().unchecked_ref()));
       onload.forget();
    }
>
</div>
}

Hope it helps!

All 2 comments

Hmm this is a classic case of jumping to wasm-bindgen instead of doing some thinking...

Did a quick google and it turns out that you can convert a blob into a typed array using a FileReader.

I'll give that a stab https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.FileReaderSync.html

Just in case anyone else comes here because they're searching about file uploads....

My use case ended up looking like this:

html! {
<div
    ondrop=move |event: web_sys::DragEvent| {
       event.prevent_default();
       event.stop_propagation();

       let store = Rc::clone(&store_clone);

       let dt = event.data_transfer().unwrap();
       let files = dt.files().unwrap();
       let psd = files.item(0).unwrap();

       let file_reader = web_sys::FileReader::new().unwrap();
       file_reader.read_as_array_buffer(&psd).unwrap();

       let mut onload = Closure::wrap(Box::new(move |event: Event| {
           let file_reader: FileReader = event.target().unwrap().dyn_into().unwrap();
           let psd = file_reader.result().unwrap();
           let psd = js_sys::Uint8Array::new(&psd);

           let mut psd_file = vec![0; psd.length() as usize];
           psd.copy_to(&mut psd_file);

           store.borrow_mut().msg(&Msg::ReplacePsd(&psd_file));
       }) as Box<FnMut(_)>);

       file_reader.set_onload(Some(onload.as_ref().unchecked_ref()));
       onload.forget();
    }
>
</div>
}

Hope it helps!

Was this page helpful?
0 / 5 - 0 ratings