Closed sinitcin closed 5 years ago
If you allow your program to read multiple image
, you need to reuse the push
method of allowed_fields
to store multiple MultipartFormDataField
instances for multiple image
.
Also, I don't recommend you to use XMLHttpRequest
+ FormData
to send multipart/form-data
requests because of the poor compatibility.
Big thank you! Do I understand correctly that the request formed in this way will not be correct?
formData.append('image[]', file1)
formData.append('image[]', file2)
formData.append('image[]', file1)
formData.append('image[]', file2)
I think it is correct but you should also change MultipartFormDataField::raw("image")
to MultipartFormDataField::raw("image[]")
and change multipart_form_data.raw.remove(&"image".to_string());
to multipart_form_data.raw.remove(&"image[]".to_string());
Sorry, but no. No matter how many times I call this code, I always get the first file.
let image = multipart_form_data.raw.remove(&"image[]".to_string());
Did you input more than two MultipartFormDataField
instances to allowed_fields
?
No, I thought it was like as in README.md example
options.allowed_fields.push(MultipartFormDataField::text("array_max_length_3"));
...
if let Some(array) = array {
match array {
TextField::Single(text) => {
let _content_type = &text.content_type;
let _file_name = &text.file_name;
let _text = &text.text;
// You can now deal with the text data.
}
TextField::Multiple(texts) => {
// Because we put "array_max_length_3" field to the allowed_fields for three times, this arm will probably be matched.
for text in texts { // The max length of the "texts" variable is 3
let _content_type = &text.content_type;
let _file_name = &text.file_name;
let _text = &text.text;
// You can now deal with the text data.
}
}
}
Yes, it was.
...
options.allowed_fields.push(MultipartFormDataField::text("array_max_length_3"));
options.allowed_fields.push(MultipartFormDataField::text("array_max_length_3"));
options.allowed_fields.push(MultipartFormDataField::text("array_max_length_3"));
...
if let Some(array) = array {
match array {
TextField::Single(text) => {
let _content_type = &text.content_type;
let _file_name = &text.file_name;
let _text = &text.text;
// You can now deal with the text data.
}
TextField::Multiple(texts) => {
// Because we put "array_max_length_3" field to the allowed_fields for three times, this arm will probably be matched.
for text in texts { // The max length of the "texts" variable is 3
let _content_type = &text.content_type;
let _file_name = &text.file_name;
let _text = &text.text;
// You can now deal with the text data.
}
}
}
}
...
I understood. But if I don't know the size of the array? Should I transfer an additional field with the number of elements? Or is it possible to find out in Rust the size of the array in multipart / data?
You can rewrite your upload_multipart
function to,
#[post("/upload_multipart/<img_count>", data = "<data>")]
fn upload_multipart(content_type: &ContentType, data: Data, img_count: u32) -> RawResponse {
if img_count > MAX_IMG_COUNT {
...
}
...
for _ in 0..img_count {
options.allowed_fields.push(...);
}
...
}
God bless you. Thanks a lot. It completely solves my problem.
Hello!
I want to send array of some photos to my application using JS. For example:
And I expect the next Rust code to read the array as RawField::Multiple(raws):
And after
xhr.send
i have response 'Please input a file.'. What am I doing wrong?