title: How to properly store uploaded file from multipartform-data in Rust?
body: <p>I'm trying to build a web server in Rust, and i'm having a few issues trying to upload file into the server. With text based files it uploads fine, but whenever i try to upload other type of media (images, videos, etc), if the file is small enough, it will save, but corrupted, as showned.</p>
  <p><a href="https://i.stack.imgur.com/j2hOF.png" rel="nofollow noreferrer">original file raw data</a></p>
  <p><a href="https://i.stack.imgur.com/SBpoq.png" rel="nofollow noreferrer">file save on the server raw data</a></p>
  <p>And when the file is too big, multer-rs library panicks with &quot;received with incomplete data&quot;.</p>
  <p><a href="https://i.stack.imgur.com/BuRPD.png" rel="nofollow noreferrer">Error log</a></p>
  <pre><code>async fn parse_body(content_type: Option&lt;&amp;String&gt;, body: String) -&gt; HashMap&lt;String, String&gt; {
      match content_type {
          Some(content_type) =&gt; {
              let ct = content_type.as_str();
              if ct.contains(&quot;application/x-www-form-urlencoded&quot;) {
                  let buffer = body.replace(&quot;\r\n\r\n&quot;, &quot;&quot;);
                  let _body = from_bytes::&lt;Vec&lt;(String, String)&gt;&gt;(buffer.as_bytes()).unwrap();
                  return _body.into_iter().collect();
              }
      
              if ct.contains(&quot;multipart/form-data&quot;) {
                  let boundary = multer::parse_boundary(ct).unwrap();
                  let data = once(async move { Result::&lt;Bytes, Infallible&gt;::Ok(Bytes::from(body)) });
                  let mut multipart = multer::Multipart::new(data, boundary);
                  let mut _body: HashMap&lt;String, String&gt; = HashMap::new();
      
                  // Iterate over the fields, use `next_field()` to get the next field.
                  while let Some(mut field) = multipart.next_field().await.unwrap() {
                      // Get field name.
                      let name = field.name().unwrap().to_string();
                      // Get the field's filename if provided in &quot;Content-Disposition&quot; header.
                      //
                      // Process the field data chunks e.g. store them in a file.
                      while let Some(chunk) = field.chunk().await.unwrap() {
                          // Do something with field chunk.
                          if let Some(file_name) = field.file_name() {
                              let file_dir = format!(&quot;src\\static\\temp\\{}&quot;, file_name);
                              let current_dir: &amp;Path = Path::new(&amp;file_dir);
                              let path = env::current_dir().unwrap().join(current_dir);
                              if let Ok(mut file) = std::fs::File::create(path) {
                                  file.write_all(&amp;chunk).unwrap();
                              }
                          } else {
                              _body.insert(name.clone(), String::from_utf8(chunk.to_vec()).unwrap());
                          }
                      }
                  }
      
                  return _body;
              }
      
          },
          None =&gt; return HashMap::new()
      }
  
      HashMap::new()
  }
  </code></pre>