svelte-file-dropzone icon indicating copy to clipboard operation
svelte-file-dropzone copied to clipboard

Multiple False Doesn't Work

Open ghost opened this issue 2 years ago • 2 comments

I'm using the original code from the documentation and just adding multiple="false":

    <Dropzone multiple="false" on:drop={handleFilesSelect} />
    <ol>
        {#each files.accepted as item}
            <li>{item.name}</li>
        {/each}
    </ol>

but it doesn't seem to work? The list inside ol element still increase even with multiple="false". Here is the result: image

ghost avatar Jun 26 '23 05:06 ghost

Wait actually after more testing, the multiple="false" made you unable to SELECT more than 1 file. So the problem might lies in this code instead:

<ol>
    {#each files.accepted as item}
        <li>{item.name}</li>
    {/each}
</ol>

Which for some reason doesn't replace the old accepted files when multiple is false.

ghost avatar Jun 26 '23 16:06 ghost

Can you please provide PR with the fix?

thecodejack avatar Jul 09 '23 12:07 thecodejack

I don't know if you figured this out but you need to set multiple={false}

<div class="">
  <Dropzone
      on:drop={handleFilesSelect}
      on:droprejected={handleFilesRejected}
      accept=".xls,.xlsx,.csv"
      multiple={false}
  />
  <ol>
	{#each files.accepted as item}
		<li>{item.name}</li>
		<li>{item.size}</li>
	{/each}
  </ol>
  </div>

I also added some logic to my functions in case they try to drag multiple files in to the drag and drop. In this case I am using swal to alert if more than one file is added.

function handleFilesSelect(e) {
		const { acceptedFiles, fileRejections } = e.detail;
		// Clear the accepted files array before adding the new file
		files.accepted = acceptedFiles;
		files.rejected = fileRejections;
	}

function handleFilesRejected(event) {
        const { file, errors } = event.detail.fileRejections[0];
        if (errors[0].message === 'Too many files') {
	        Swal.fire({
		        icon: 'error',
		        title: `Only one file can be imported at a time!`,
		        background: `${backgroundColor}`,
		        color: `${textColor}`
	        });
        } else {
	        Swal.fire({
		        icon: 'error',
		        title: `File ${file.name} was rejected: ${errors[0].message}`,
		        background: `${backgroundColor}`,
		        color: `${textColor}`
	        });
        }
}

nicholasvperry avatar Apr 04 '24 18:04 nicholasvperry

multiple={false} should work

arackaf avatar Apr 06 '24 23:04 arackaf