Sara
Sara

Reputation: 181

Compress PSD and AI files on client side (javascript) on uploading

I’m publishing posts (with PSD files), these files are very large in size, so I tried to compress them once I upload the file (and before publishing the post), I used jszip package and it worked for me but unfortunately I can't modify the FileList() object to assign the new compressed file to the file field without using (Data Transfer) which is not supported in Safari and IE.

let zip = new JSZIP()
zip.file(filename, file_input_field.files[0])
zip.generateAsync({type:"blob"}).then((blobdata)=>{
let zipblob=new DataTransfer();
let zip_file=new File([zipblob], “filename.zip”);
list.items.add(zip_file);
let myFileList = list.files;
file_input_field.files = myFileList

I appreciate you help!

Upvotes: 0

Views: 140

Answers (1)

kosmos
kosmos

Reputation: 4288

The files attribute is a FileList object, you can't modify it.

In your case I would use the FormData interface and send the form asynchronously. You've a clear example at this page: Using FormData Objects - MDN.

In the example they use XMLHttpRequest but you can also use the Fetch API. See also: Using Fetch.

Upvotes: 1

Related Questions