1

Good morning. I am trying to copy a file to a page that receives it, this works fine in chrome, edge and opera, but not in firefox....

The idea is to copy one or several files and to be able to paste them in the browser, the function below recognizes the paste effect, consults the clipboardData and inserts these images or videos in some input files.

The only browser that is not supported is Firefox, and version 83 of chrome as far as I have tested.

window.addEventListener("paste",copyPasteFunction); 

function copyPasteFunction(e){
        var fileInput = document.querySelector("input");
        if(e.clipboardData.files.length > 0){
            fileInput.files = e.clipboardData.files;
            $("input").eq(0).trigger("change");        
        }else{            
            console.log("%cFirefox or unsupported browser", "color:red");
        }
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file">

¿Does anyone know of any other method to copy a file, paste it into a page and have it read and inserted into an input?

Thank you very much!

vanowm
  • 9,466
  • 2
  • 21
  • 37
  • _"The [`DataTransfer.files`](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/files) property is a list of the files **in the drag operation**. If the operation includes no files, the list is empty."_ - If that works with `paste` in Chromium then I would say that Chromium is the one whose wrong... – Andreas Dec 23 '21 at 13:01
  • @Andreas where did you get DataTransfer here? – vanowm Dec 23 '21 at 13:03
  • 1
    _"The [`ClipboardEvent.clipboardData`](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData) property holds a [`DataTransfer`](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer) object, which can be used..."_ - But I guess the _"in the drag operation"_ is not relevant in this case so please ignore that. – Andreas Dec 23 '21 at 13:05
  • 1
    Something tells me this falls into the same category as [`readText()`](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText) which only supported by extensions? – vanowm Dec 23 '21 at 13:14

0 Answers0