More fullstack fixes (#4737)
* allow `--server` only builds! * rollback example * capture `/:route` routes * fix: properly implement file uploads in forms * implement desktop form uploads * Performant serialization of form bytes * finish up file stuff * cleanups * add request headers to streaming context * remove some unwraps
This commit is contained in:
1 parent
fc01a002fd
commit
92792aded9
47 files changed
+1241
-703
No files matched your search
@@ -161,6 +161,49 @@ impl ClientRequest {
|
||||
builder
|
||||
}
|
||||
|
||||
/// Sends the request with multipart/form-data body constructed from the given FormData.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub async fn send_multipart(
|
||||
self,
|
||||
form: &dioxus_html::FormData,
|
||||
) -> Result<ClientResponse, RequestError> {
|
||||
let mut outgoing = reqwest::multipart::Form::new();
|
||||
|
||||
for (key, value) in form.values() {
|
||||
match value {
|
||||
dioxus_html::FormValue::Text(text) => {
|
||||
outgoing = outgoing.text(key.to_string(), text.to_string());
|
||||
}
|
||||
dioxus_html::FormValue::File(Some(file_data)) => {
|
||||
outgoing = outgoing
|
||||
.file(key.to_string(), file_data.path())
|
||||
.await
|
||||
.map_err(|e| {
|
||||
RequestError::Builder(format!(
|
||||
"Failed to add file to multipart form: {e}",
|
||||
))
|
||||
})?;
|
||||
}
|
||||
dioxus_html::FormValue::File(None) => {
|
||||
// No file was selected for this input, so we skip it.
|
||||
outgoing = outgoing.part(key.to_string(), reqwest::multipart::Part::bytes(b""));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let res = self
|
||||
.new_reqwest_request()
|
||||
.multipart(outgoing)
|
||||
.send()
|
||||
.await
|
||||
.map_err(reqwest_error_to_request_error)?;
|
||||
|
||||
Ok(ClientResponse {
|
||||
response: Box::new(res),
|
||||
extensions: self.extensions,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn send_form(self, data: &impl Serialize) -> Result<ClientResponse, RequestError> {
|
||||
// For GET and HEAD requests, we encode the form data as query parameters.
|
||||
// For other request methods, we encode the form data as the request body.
|
||||
|
||||
@@ -95,7 +95,11 @@ impl<S> IntoRequest for MultipartFormData<S> {
|
||||
// and construct a multipart form body manually.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
todo!("Properly implement multipart form data for non-web wasm32 clients");
|
||||
let data = self._client.clone().ok_or_else(|| {
|
||||
RequestError::Builder("Failed to get FormData from event".into())
|
||||
})?;
|
||||
|
||||
return _req.send_multipart(&data).await;
|
||||
}
|
||||
|
||||
unimplemented!("Non web wasm32 clients are not supported yet")
|
||||
|
||||
Reference in new issue
Block a user