1

I have a function that uploads a CSV file into a server via form submission. It is working well, but I need to support copy and paste of CSV file from the actual file location to the input element.

I tried to do this code from a stackoverflow answer (https://stackoverflow.com/a/50427897/9751944)

const form = document.getElementById("new_document_attachment");
const fileInput = document.getElementById("document_attachment_doc");

fileInput.addEventListener('change', () => {
  form.submit();
});

window.addEventListener('paste', e => {
  fileInput.files = e.clipboardData.files;
});
<form id="new_document_attachment" method="post">
  <div class="actions"><input type="submit" name="commit" value="Submit" /></div>
  <input type="file" id="document_attachment_doc" />
</form>

It is working on images. But when I try it on CSV and other files, I am getting an error saying that the clipboard does not have any content ie.

The first FileList and File are for the test image while the second one is for CSV

The first FileList and File are for the test image while the second one is for CSV

  • I fear it is an OS limitation... When looking at my clipboard data on osX Finder>Edit>Show Clipboard all I see is the file name of non image/* files. Same way, the default value that gets inserted by the event is the file's name. Whereas with an image, it's the image that gets pasted. – Kaiido Oct 26 '18 at 07:09
  • I see. Sad to hear that @Kaiido . Let us wait for other answers, maybe someone has a workaround or something in mind. Thanks anyways! :D – French Clifford Dacion Oct 26 '18 at 09:02
  • Just to confirm, it's a OS limitation. If you do the same on Slack or Telegram, they end up with same png. Too bad... – Ross Fine Mar 24 '20 at 17:28

0 Answers0