我是靠谱客的博主 俏皮热狗,最近开发中收集的这篇文章主要介绍Rust reqwest 多文件上传至其他服务端,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

tokio = { version = "1.5", features = ["macros", "rt-multi-thread"] }
hex = "0.4.3"
bytes = "1.1.0"
eyre = "0.6.8"
ethers-contract = { version = "^0.17.0", default-features = false, features = [
    "abigen",
    "eip712"
] }
ethers-addressbook = "0.17.0"
chrono = "0.4.22"
reqwest = { version = "0.11", features = ["json","stream","blocking","multipart"] }
dotenvy = "0.15.5"
tokio-util = { version = "0.7.4", features = ["codec"] }
//单文件上传
async fn reqwest_multipart_form_single(url:&str) ->Result<()> {

    let client = Client::new();
    
    //路径和 cargo.toml 在一个目录下面
    let file2 = File::open("test.jpg").await?;
    
    let file_body2 = file_to_body(file2);

    let bio2 = multipart::Part::stream(file_body2)
    .file_name("test.jpg")
    .mime_str("image/jpeg")?;

    let form = multipart::Form::new()
    .part("files", bio2);

    let mut headers_data = HeaderMap::new();
    // headers_data.insert("Content-Type", "multipart/form-data".parse()?);
    headers_data.insert("Token", "".parse()?);
    let response = client.post(url).headers(headers_data).multipart(form).send().await?.text().await?;
    
    // let print_string = response.text().await?;
    println!("res-----{:?}",response   );
    // let result = response.json::<HttpBinResult>().await?;
    // // let result = HttpBinResult { args: None, data:None, files:None, form:None, headers: None, json: None, origin: None, url: None };
    // Ok(result)
    Ok(())
}


//视频和图片 同时上传
async fn reqwest_multipart_form(url:&str) ->Result<()> {

    let client = Client::new();
    
    
    //路径和 cargo.toml 在一个目录下面
    let file1 = File::open("test.mp4").await?;
    let file2 = File::open("test.jpg").await?;
    
    let file_body1 = file_to_body(file1);
    let file_body2 = file_to_body(file2);

    let bio1 = multipart::Part::stream(file_body1)
    .file_name("test.mp4")
    .mime_str("video/mp4")?;
    let bio2 = multipart::Part::stream(file_body2)
    .file_name("test.jpg")
    .mime_str("image/jpeg")?;

    let form = multipart::Form::new()
    .part("files", bio1)
    .part("files", bio2);

    let mut headers_data = HeaderMap::new();
    // headers_data.insert("Content-Type", "multipart/form-data".parse()?);
    headers_data.insert("Token", "".parse()?);
    let response = client.post(url).headers(headers_data).multipart(form).send().await?.text().await?;
    
    // let print_string = response.text().await?;
    println!("res-----{:?}",response   );
    // let result = response.json::<HttpBinResult>().await?;
    // // let result = HttpBinResult { args: None, data:None, files:None, form:None, headers: None, json: None, origin: None, url: None };
    // Ok(result)
    Ok(())
}


fn file_to_body(file: File) -> Body {
    let stream = FramedRead::new(file, BytesCodec::new());
    let body = Body::wrap_stream(stream);
    body
}

 

 

最后

以上就是俏皮热狗为你收集整理的Rust reqwest 多文件上传至其他服务端的全部内容,希望文章能够帮你解决Rust reqwest 多文件上传至其他服务端所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部