pqina / filepond-plugin-file-rename

🏷 File rename plugin for FilePond
MIT License
6 stars 3 forks source link

Using the fileRenameFunction changes "File" to "Blob" #15

Closed redfellow closed 3 years ago

redfellow commented 3 years ago

Simply initing filePond with fileRenameFunction: function(file) { return file.name } changes how the file appears in formData.

image

Should look like this (no function set): image

The renaming does work UI-wise, the preview box has the correct filename, it's just when starting the actual upload, things break.

Output of pond.getFiles() when function is in use:

image

And when not in use:

image

So from quick look, it looks like when renamer is in use, the file object in getfiles is actually a blob and not a File (instance of a blob)?

Any way to fix this?

rikschennink commented 3 years ago

How are you uploading the file?

(it turns it into a blob because file constructor isn't supported on old IE)

redfellow commented 3 years ago
server: {
    process: component.processFile,
}

And the upload logic is our own. It's basically and endpoint that eats multiple formData objects containing a File each.

Snip:

            var xhr = new XMLHttpRequest();

            xhr.open("POST", parameters.serviceurl, true);

            xhr.onload = function (error) {
                ...
            };

            xhr.upload.onprogress = function (e) {
                ...
            };

            xhr.onerror = function (error) {
                ...
            }

            xhr.send(formData);

The File object limitation/requirement comes from the server side. We could perhaps work around this. It's still confusing as to why the rename plugin turns the File object inside fileponds getFiles to a Blob.

Edit: I might of just now understood your comment, the renaming of a file creates a new File, but since this isn't supported on IE, the renamer uses just a Blob for compatability?

rikschennink commented 3 years ago

Because renaming a file requires creating a new file. https://pqina.nl/blog/rename-a-file-with-javascript/

How are you creating the formData, the approach below should work.

const formData = new FormData();
formData.append(fieldName, file, file.name);
redfellow commented 3 years ago

Thanks for setting me on correct path. I refactored the upload logic to use that style of formData.append vs. just key/value that we had.