@@ -363,10 +363,8 @@ pub fn FileUpload() -> impl IntoView {
363
363
Ok ( count)
364
364
}
365
365
366
- let upload_action = Action :: new_local ( |data : & FormData | {
367
- // `MultipartData` implements `From<FormData>`
368
- file_length ( data. clone ( ) . into ( ) )
369
- } ) ;
366
+ let pending = RwSignal :: new ( false ) ;
367
+ let result = RwSignal :: new ( None ) ;
370
368
371
369
view ! {
372
370
<h3>File Upload </h3>
@@ -375,22 +373,26 @@ pub fn FileUpload() -> impl IntoView {
375
373
ev. prevent_default( ) ;
376
374
let target = ev. target( ) . unwrap( ) . unchecked_into:: <HtmlFormElement >( ) ;
377
375
let form_data = FormData :: new_with_form( & target) . unwrap( ) ;
378
- upload_action. dispatch_local( form_data) ;
376
+ pending. set( true ) ;
377
+ spawn_local( async move {
378
+ result. set( Some ( file_length( form_data. into( ) ) . await ) ) ;
379
+ pending. set( false ) ;
380
+ } ) ;
379
381
} >
380
382
<input type ="file" name="file_to_upload" />
381
383
<input type ="submit" />
382
384
</form>
383
385
<p>
384
386
{ move || {
385
- if upload_action . input_local ( ) . read ( ) . is_none ( ) && upload_action . value ( ) . read( ) . is_none( )
387
+ if !pending . get ( ) && result . read( ) . is_none( )
386
388
{
387
389
"Upload a file." . to_string( )
388
- } else if upload_action . pending( ) . get( ) {
390
+ } else if pending. get( ) {
389
391
"Uploading..." . to_string( )
390
- } else if let Some ( Ok ( value) ) = upload_action . value ( ) . get( ) {
392
+ } else if let Some ( Ok ( value) ) = result . get( ) {
391
393
value. to_string( )
392
394
} else {
393
- format!( "{:?}" , upload_action . value ( ) . get( ) )
395
+ format!( "{:?}" , result . get( ) )
394
396
}
395
397
} }
396
398
0 commit comments