从 Rust 中的 web_sys::File 获取参数

发布于 2025-01-09 07:22:52 字数 778 浏览 5 评论 0原文

我正在从 JavaScript 发送 FileList 并尝试从列表中读取特定文件的参数(例如文件名),但收到错误:在 Option中找不到方法 (我尝试了不同的变体来调用文件的 getter 方法,如 web-sys doc 但没有成功)。

#[wasm_bindgen]
pub fn get_file_list_detail(files : web_sys::FileList) -> Option<web_sys::File> {
    let first_file = files.get(1);
    log!("Test console log from rust {:?}=",first_file.name()); //this is not working
    return first_file;
}

我在 Cargo.toml 中添加了 FileFileList

[dependencies.web-sys]
version = "0.3"
features = [
    "HtmlInputElement",
    "FileList",
    "File",
    "console"
]

I'm sending a FileList from JavaScript and trying to read the parameters of the specific file from the list, like the file name but I'm getting the error:method not found in Option<web_sys::File> (I'have tried different variants to call the getter methods of the File like defined in the web-sys doc but no success).

#[wasm_bindgen]
pub fn get_file_list_detail(files : web_sys::FileList) -> Option<web_sys::File> {
    let first_file = files.get(1);
    log!("Test console log from rust {:?}=",first_file.name()); //this is not working
    return first_file;
}

I added the File and FileList in Cargo.toml:

[dependencies.web-sys]
version = "0.3"
features = [
    "HtmlInputElement",
    "FileList",
    "File",
    "console"
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2025-01-16 07:22:52

files.get(1)< /code>返回 Option ,它可以是 某些(文件) 变体。您可以使用 match 语句来匹配这些变体并采取相应的操作。

#[wasm_bindgen]
pub fn get_file_list_detail(files : web_sys::FileList) -> Option<web_sys::File> {
    let first_file = files.get(1);
    match first_file {
        Some(ref file) => {
            log!("Test console log from rust {:?}=",file.name());
        },
        None => {
            log!("file is Missing")
        }
    }
    
    return first_file;
}

files.get(1) returns Option<File> which could be either None or Some(File) variant. You can use match statement to match these variants and take actions accordingly.

#[wasm_bindgen]
pub fn get_file_list_detail(files : web_sys::FileList) -> Option<web_sys::File> {
    let first_file = files.get(1);
    match first_file {
        Some(ref file) => {
            log!("Test console log from rust {:?}=",file.name());
        },
        None => {
            log!("file is Missing")
        }
    }
    
    return first_file;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文