yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.23k stars 1.41k forks source link

Blob download encoding #3622

Closed Chtau closed 4 months ago

Chtau commented 4 months ago

Problem If a Blob is created the bytes their encoding is wrong.

Steps To Reproduce

  1. Create a Blob with a String ("Test") as content
  2. Create a Url from the Blob
    
    use web_sys::{js_sys::Uint8Array, Blob, BlobPropertyBag};
    use yew::prelude::*;

[derive(PartialEq, Properties)]

pub struct DownloadBlobProps { pub text: String, }

[function_component]

pub fn DownloadBlob(props: &DownloadBlobProps) -> Html { let array_buffer = props.text.clone().into_bytes(); let uint8_array = Uint8Array::from(array_buffer.as_slice());

let blob = Blob::new_with_u8_array_sequence_and_options(
    &uint8_array,
    BlobPropertyBag::new().type_("text/plain"),
)
.expect("Failed to create Blob");
let url = web_sys::Url::create_object_url_with_blob(&blob)
    .expect("Failed to create object URL from Blob");

html! {
    <a
        href={url}
        download="file.txt"
    >
        { "Download File" }
    </a>
}

}


4. Download the file provided by the `Url`
5. The Provided String Value "Test" is shown as "84101115116" in the File

**Expected behavior**
The File should have the provided String value as Content.

**Environment:**
 - Yew version: v0.21, master
 - Rust version: 1.75.0
 - Build tool, if relevant: trunk
 - OS: Windows 10
 - Browser and version: Brave 1.63.165 Chromium 122.0.6261.94, Firefox 123.0, Edge 122.0.2.2365.66

Minimal Example Code to reproduce: [yew-blob-download-bug.zip](https://github.com/yewstack/yew/files/14483807/yew-blob-download-bug.zip)

I also tried different methods to Encode and Blob creation but without success.