-1

I'm building an app using yew and wasm to share files locally using WebRTC, however I'm having a difficult time finding a way to properly do the different file conversions, I will be converting the uploaded file to vec to chunk them and send them, and the opposite on the receiving end

I will be using this code to initially convert a file to &[u8]

let onloadend_cb = Closure::<dyn FnMut(_)>::new(move |_e: web_sys::ProgressEvent| {
        let file_reader: FileReader = _e.target().unwrap().dyn_into().unwrap();

        let file = file_reader.result().unwrap();
        let file =  js_sys::Uint8Array::new(&file);

        let mut file_u8 = vec![0; file.length() as usize];
        file.copy_to(&mut file_u8);
        log!(file_u8.len());

        let chunks = file_u8.chunks(8).collect::<Vec<_>>();
        for chunk in chunks {
            data_channel.send_with_u8_array(chunk).expect("err data channel");
        }
 });

let file_reader = web_sys::FileReader::new().unwrap();
file_reader.read_as_array_buffer(&file).unwrap();
 onloadend_cb.forget();

on the receiving end


//convert arraybuffer to vector
//merge the chunks 
// convert the resulted vector back to an array
// convert the array to Bolb or File and prompt the user to download

I feel my method is not efficient, the browser start to become slow when I send a large file, also my attempt to construct File back from Array was not successful

I'd appreciate any help on how to properly excute this

abdesamed
  • 34
  • 1
  • 3

0 Answers0