【问题标题】:How to convert a string to Blob in web_sys?如何在 web_sys 中将字符串转换为 Blob?
【发布时间】:2023-01-25 23:25:13
【问题描述】:

我正在尝试使用 web_sys 将字符串转换为 blob


let json_string = json::stringify(data);
let json_jsvalue = JsValue::from_str(&json_string);

let json_blob_result = Blob::new_with_str_sequence(&json_jsvalue);
let json_blob = json_blob_result.unwrap();

它给出了错误:

Err 值上调用 Result::unwrap() 时惊慌失措:JsValue(TypeError: Blob 构造函数: 参数 1 无法转换为序列。

【问题讨论】:

    标签: rust web-sys


    【解决方案1】:

    As explained in MDN,要从字符串创建Blob,您需要将其包装在数组中:

    let json_string = json::stringify(data);
    let json_jsvalue = JsValue::from_str(&json_string);
    let json_jsvalue_array = js_sys::Array::from_iter(std::iter::once(json_jsvalue));
    
    let json_blob_result = Blob::new_with_str_sequence(&json_jsvalue_array);
    let json_blob = json_blob_result.unwrap();
    

    【讨论】:

      猜你喜欢
      • 2013-06-28
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2020-01-19
      • 2017-07-07
      • 2021-08-19
      相关资源
      最近更新 更多