1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rust - Actix-web - Zipファイルダウンロードコード

Last updated at Posted at 2021-06-03

Actix-webでファイルダウンロードコードです

Cargo インストール

zip = "0.5.12"

コード書く

#[get("report")]
async fn execute(
// 何かする
) -> Result<HttpResponse, ApiError> {

    let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));

    let options = zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
    zip.start_file("hello_world.csv", options)?;
    zip.write(b"Hello, World!")?;
    zip.flush()?;
    let result = zip.finish()?;
    Ok(
        HttpResponse::Ok()
        .header("content-encoding", "application/zip")
        .header("Content-Disposition", "attachment;filename=sampleZip.zip")
        .body(result.into_inner())
    )

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?