Comments (2)
可以讲一下怎么上传文件吗?
To test this example:
- $ cargo run --example upload
- $ curl -T ./README.md localhost:8080 # this writes the file to a temp directory
- $ curl localhost:8080/README.md # this reads the file from the same temp directory
use std::io::Error as IoError;
use std::path::Path;
use std::sync::Arc;
use async_std::{fs::OpenOptions, io};
use tempfile::TempDir;
use tide::prelude::*;
use tide::{Body, Request, Response, StatusCode};
#[derive(Clone)]
struct TempDirState {
tempdir: Arc<TempDir>,
}
impl TempDirState {
fn try_new() -> Result<Self, IoError> {
Ok(Self {
tempdir: Arc::new(tempfile::tempdir()?),
})
}
fn path(&self) -> &Path {
self.tempdir.path()
}
}
#[async_std::main]
async fn main() -> Result<(), IoError> {
tide::log::start();
let mut app = tide::with_state(TempDirState::try_new()?);
app.at(":file")
.put(|req: Request<TempDirState>| async move {
let path = req.param("file")?;
let fs_path = req.state().path().join(path);
let file = OpenOptions::new()
.create(true)
.write(true)
.open(&fs_path)
.await?;
let bytes_written = io::copy(req, file).await?;
tide::log::info!("file written", {
bytes: bytes_written,
path: fs_path.canonicalize()?.to_str()
});
Ok(json!({ "bytes": bytes_written }))
})
.get(|req: Request<TempDirState>| async move {
let path = req.param("file")?;
let fs_path = req.state().path().join(path);
if let Ok(body) = Body::from_file(fs_path).await {
Ok(body.into())
} else {
Ok(Response::new(StatusCode::NotFound))
}
});
app.listen("127.0.0.1:8080").await?;
Ok(())
}
from tide-async-graphql-mongodb.
谢谢
from tide-async-graphql-mongodb.
Related Issues (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 tide-async-graphql-mongodb.