Please help me to understand file upload

use actix_multipart::Multipart;
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use futures::stream::StreamExt;
// use std::fs::File;
use std::{io, str};

 while let Some(item) = payload.next().await {
        let field = item.unwrap();

        let content_type = field.content_disposition();
        let filename = content_type.get_filename().unwrap();

        // let mut file = File::create(format!("pub/{}", filename)).unwrap();
        // file.write_all(parts[0].data().await.unwrap().unwrap().bytes())
        // .unwrap();

        println!("content_type: {}", content_type);
        println!("file name: {}", filename);

        // while let Some(chunk) = field.next().await {
        //     println!("-- CHUNK: \n{:?}", str::from_utf8(&chunk.unwrap()));
        // }
    }

    HttpResponse::Ok().body("Done")

This above code is working fine.
But How can I get the file & check mime type & size & other meta data & move the file to another directory?
Thank you

Huh?

Well, there are some heuristics, but in general you can't tell. The most common heuristic is to look at the first few bytes of a file (aka, the magic bytes). This is how the Unix file command works.

I would suggest you actually go through the std-lib docs. It's more useful than you think.

I got the solution

 while let Some(item) = payload.next().await {
        let mut field = item.unwrap();

        let content_type = field.content_disposition();

        println!("{}", field.name());
        println!("CONTENT TYPE: {}", content_type);
        println!("FILE NAME: {}", content_type.get_filename().unwrap());
        println!("UPLOAD NAME FIELD: {}", content_type.get_name().unwrap());
        println!("MIME TYPE: {}", field.content_type());
        println!("FIELD: {:?}", field);

        let file_path = format!("pub/{:?}.jpg", time::Instant::now());
        let mut create_file = File::create(file_path).unwrap();

        while let Some(chunk) = field.next().await {
            let data = chunk.unwrap();
            create_file.write_all(&data).unwrap();
        }
    }

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.