Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify returning a File or Blob? #146

Open
walpolsh opened this issue Apr 5, 2022 · 5 comments
Open

How to specify returning a File or Blob? #146

walpolsh opened this issue Apr 5, 2022 · 5 comments

Comments

@walpolsh
Copy link

walpolsh commented Apr 5, 2022

I'm just curious how to specify if a file or a blob is returned upon success?

This is how I'm using it:

export async function compress(file, quality, maxHeight, maxWidth) {
  return await new Promise((resolve, reject) => {
    new Compressor(file, {
      quality,
      maxHeight,
      maxWidth,
      success: resolve,
      error: reject,
    });
  });
}
const fullImage = await compress(fileObj, 0.7)
const thumbnail = await compress(fileObj, 0.7, 256, 256) 

In both cases I get a Blob back but would rather it remain file without doing another conversion

Thanks!

@fengyuanchen
Copy link
Owner

Currently not supported.

@VisionaryAppDev
Copy link

Hello @fengyuanchen,

I have tested this lib for a while on Nuxt 2 and it's working pretty well. But, there is some problem when using it on Nuxt 3 and I couldn't get it working. I have a very similar code to the one you provided inside docs directory on github.

The result is success, no error log inside console log and the vm.output data is updated to the new value (I have verified it through Vue DevTools) but there isn't anything changed to the browser/ui.

options: {
         ...
          success: function (result) {
            vm.output = result;
          },
          ...
        },

Not sure if you have meet this problem before. I couldn't figure out what is going on now.

@fengyuanchen
Copy link
Owner

@VisionaryAppDev Sorry, I haven't used Nuxt.

@Murtdha
Copy link

Murtdha commented May 8, 2023

Hello @fengyuanchen,

I have tested this lib for a while on Nuxt 2 and it's working pretty well. But, there is some problem when using it on Nuxt 3 and I couldn't get it working. I have a very similar code to the one you provided inside docs directory on github.

The result is success, no error log inside console log and the vm.output data is updated to the new value (I have verified it through Vue DevTools) but there isn't anything changed to the browser/ui.

options: {
         ...
          success: function (result) {
            vm.output = result;
          },
          ...
        },

Not sure if you have meet this problem before. I couldn't figure out what is going on now.

can you provided me the code

@nik32
Copy link

nik32 commented May 15, 2023

A Temporary solution until this enhancement is added to the lib -

const removeExtension = (fileName: string) =>
  fileName.substring(0, fileName.lastIndexOf('.')) || fileName;

export async function compress(file, quality, maxHeight, maxWidth) {
  return await new Promise((resolve, reject) => {
    new Compressor(file, {
      quality,
      maxHeight,
      maxWidth,
      success: (compressedFile) => {
        if (compressedFile instanceof File) return resolve(compressedFile);
        else {
          const compressedFileFromBlob = new File([compressedFile], removeExtension(file.name), {
            type: compressedFile.type,
          });
          return resolve(compressedFileFromBlob);
        }
      },
      error: reject,
    });
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants