简体   繁体   English

是否可以在上传到服务器之前更改为图像的字节大小或宽度和高度?

[英]Is it possible to change to byte size or width and height of an image before uploading to the server?

Is there a way to change the byte size of the file?有没有办法改变文件的字节大小? For example, below I'm checking to see if the file size that's being uploaded is > than a max size (variable coming from backend).例如,下面我正在检查正在上传的文件大小是否大于最大大小(来自后端的变量)。

I've seen examples online that use canvas but I don't want to use that.我在网上看到使用canvas的示例,但我不想使用它。 Below, my attempt doesn't seem to be working.下面,我的尝试似乎没有奏效。

Is it possible for me to change the image's dimensions (before the if() statement) so that naturally the MB will decrease as well?我是否可以更改图像的尺寸(在if()语句之前),这样 MB 也会自然减少? (width and height) before uploading to the server? (宽度和高度)上传到服务器之前?

I've tried a lot of ways but have ran out options.我已经尝试了很多方法,但已经用完了选项。

function fileValidationWinnerPhoto() {
    const realFileBtn = document.getElementById("real-file");
    let filePath = realFileBtn.value;
    let maxSize = size * 1000000; // 10MB
    
    realFileBtn.files[0].size = 2097152;

    if(realFileBtn.files[0].size > maxSize) {
        console.log(realFileBtn.files[0].size);

        fileSizeErrorModal.style.display = "block";
        attachEventListenerToFileSizeErrorModal(customButtonFileSize, fileSizeErrorModal)
        realFileBtn.value = '';
        return false;
    } else {
        fileAcceptedFlag = true;
    }
    return filePath;
}

Are you using Node?你在用节点吗? You could use Sharp https://www.npmjs.com/package/sharp您可以使用夏普https://www.npmjs.com/package/sharp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM