Menu

Let users retry the same photo in a JavaScript file picker
📰

Let users retry the same photo in a JavaScript file picker

DEV Community: frontend·David Miller·6 days ago
#SOumOe2X
#devtofrontend#devto#dev#file#picker
Reading 0:00
15s threshold

Select a photo, simulate a failed upload, and select the same photo again. If the input still holds that file, the browser can treat the second selection as a cancellation, so a change handler may never run.

For a JavaScript-managed queue, take a reference to the File before clearing the picker. In this example addPhotoToQueue is the application function that stores the file.

<label for = "photo" > Add a photo </label> <input id = "photo" type = "file" accept = "image/*" >

Enter fullscreen mode

Exit fullscreen mode

const picker = document . querySelector ( "#photo" ) ; picker . addEventListener ( "change" , ( ) => { const file = picker . files [ 0 ] ; if ( ! file ) return ; picker . value = "" ; addPhotoToQueue ( file ) ; } ) ;

Enter fullscreen mode

Exit fullscreen mode

Show the filename from the queue once the picker resets.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.