Comments (4)
It's really much easier than that:
[dependencies]
multer = { version = "2.1.0", features = ["tokio-io"] }
tokio = { version = "1.32.0", features = ["full"] }
use tokio::io;
use multer::Multipart;
#[tokio::main]
async fn main() -> multer::Result<()> {
let mut multipart = Multipart::with_reader(io::stdin(), "X-BOUNDARY");
while let Some(mut field) = multipart.next_field().await? {
while let Some(chunk) = field.chunk().await? {
println!("chunk: {:?}", chunk);
}
}
Ok(())
}
> echo "--X-BOUNDARY\r\nContent-Disposition: form-data; name=\"my_text_field\"\r\n\r\nabcd\r\n--X-BOUNDARY--\r\n" | cargo run
Chunk: b"abcd"
from multer-rs.
So close, but so far. ReceiverStream provides ReceiverStream as Stream ...I need a Result<O, E>.
use futures_util::stream::StreamExt;
use tokio::io;
use async_stdin::recv_from_stdin;
use tokio_stream::wrappers::ReceiverStream;
use multer::Multipart;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rx = recv_from_stdin(10);
let stream = ReceiverStream::new(rx);
let boundary = "X-BOUNDARY";
// Create a Multipart instance from the byte stream and boundary.
let mut multipart = Multipart::new(stream, boundary);
while let Some(field) = multipart.next_field().await? {
let name = field.name();
let file_name = field.file_name();
println!("Name: {:?}, File Name: {:?}", name, file_name);
// Read field content as bytes.
let mut bytes = Vec::new();
//field.copy_to(&mut bytes).await?;
// Convert bytes to a UTF-8 string.
let content = String::from_utf8_lossy(&bytes).to_string();
println!("Content: {:?}", content);
}
Ok(())
}
error[E0271]: type mismatch resolving `<ReceiverStream<String> as Stream>::Item == Result<_, _>`
--> examples\simple_stdin.rs:16:40
|
16 | let mut multipart = Multipart::new(stream, boundary);
| -------------- ^^^^^^ expected `Result<_, _>`, found `String`
| |
| required by a bound introduced by this call
|
= note: expected enum `Result<_, _>`
found struct `String`
note: required by a bound in `Multipart::<'r>::new`
--> c:\working\rust\multer-rs\src\multipart.rs:107:19
|
105 | pub fn new<S, O, E, B>(stream: S, boundary: B) -> Self
| --- required by a bound in this associated function
106 | where
107 | S: Stream<Item = Result<O, E>> + Send + 'r,
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `Multipart::<'r>::new`
from multer-rs.
oh man, so very nice! that worked like a charm. thank you.
from multer-rs.
de-vri-es/webhook-httpd@7bddfcb
You're welcome to the example if it might help others. very cool.
from multer-rs.
Related Issues (20)
- The size limit should be `u64` rather than `usize` type.
- Use std::u64::MAX instead of u64::MAX as u64::MAX depends on rustc 1.43.0 HOT 1
- Unable to support utf8 file name HOT 8
- Support for async-std HOT 2
- "Multipart stream is incomplete" when form data has leading newlines. HOT 7
- Couldn't work with bytes 1.0 HOT 2
- Does this crate work with multipart/mixed ? HOT 1
- Consider allowing whitespaces after the boundary and before the CRLF HOT 1
- New release v2.0.0 ? HOT 6
- Remove `format!` allocations during parsing HOT 2
- Doesn't support 'name=' without double quotes
- Release 2.0.3 HOT 3
- Supporting unquoted name and filename broke semicolons in filenames HOT 1
- Clarify Field.name in relation to the RFC HOT 1
- support `filename*` HOT 3
- Not extracting `file_name` correctly if filename contains double quotes
- No release entry for v3.0.0 HOT 2
- StreamBuffer can use unlimied memory if the underlying stream is always ready HOT 3
- Provide accurate error information. HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from multer-rs.