Create a file from a URL to assign a file input

setFiletoFileInput: function (url, fileName) {
                $.ajax({
                    url: url,
                    method: 'GET',
                    xhrFields: {
                        responseType: 'blob'
                    },
                    success: function (data) {
                        // Create a File object from the Blob
                        const file = new File([data], fileName, { type: data.type });

                        // Create a new file input element
                        const $fileInput = jQuery('.logoInfo input[name="fileuploadfield"]');

                        // Create a new FileList containing the File object
                        const fileList = new DataTransfer();
                        fileList.items.add(file);

                        // Add the File object to the file input
                        // Create a new FileList containing the File object
                        jQuery('.logoInfo input[name="fileuploadfield"]')[0].files = fileList.files;


                        // Append the file input element to the body

                    },
                    error: function (xhr, status, error) {
                        console.error('Error fetching and creating file:', error);
                    }
                });
            }

Leave a comment

Your email address will not be published. Required fields are marked *