在MeteorJS中上传图像文件

Image File upload in MeteorJS

本文关键字:图像 文件 MeteorJS      更新时间:2023-09-26

试图找到正确的方法来上传文件,即用户个人资料图片,并使用MeteorJS保存到服务器。

几个月前我尝试了 CollectionFS,它在文件保存到服务器文件系统之前刷新了浏览器/Meteor 网页,最终显示了一个损坏的图像链接。

我读了这篇文章,但我不想使用 S3,并希望确保避免断开的链接问题。也许我需要一个回调来告诉 Meteor 在服务器上完全写入(并调整大小)之前不要刷新网页:MeteorJS图片上传/转换

对于小型文件,一种方法是将它们另存为 base64 字符串。检索速度超快。

要读取文件,您可以使用拖放或选择文件,然后使用普通的javascript,例如

var reader = new FileReader();
                        var filename = file.name;
                        reader.readAsDataURL(file);
                        reader.onload = function (imgsrc) {
                            var fileVar = imgsrc.target.result;
                            $("#FileUploadingAttachmentNewWay").val(fileVar);
                            $("#FileUploadingAttachmentNameNewWay").val(file.name);
                            $("#FileUploadingAttachmentNewWay").click();
                        }

要回读,您需要从 base64 更改 if 并将其放入您单击并下载的临时元素中(全部在后台)。在 meteor 中注意到,如果文件大小大于 4-5mb,则集合可能会损坏,因此我们对图像大小有限制。

下面的代码供您在 a 中分配 base64 并下载它。它的质量不好,部分来自教程,但你会明白这一点(它来自一个旧的测试项目)。

function download(strData, strFileName, strMimeType) {
        var D = document,
            A = arguments,
            a = D.createElement("a"),
            d = A[0],
            n = A[1],
            t = A[2] || "application/octet";
        //debugger;
        //build download link:
        a.href = strData.replace("data:;", "data:" + strMimeType + ";").replace("data:text/plain;", "data:" + strMimeType + ";");
        //trying to avoid chrome specific crash (works ok on firefox though) issue 69227 google chrome====>
        //convert to binary in ArrayBuffer this needs change
        var binStr1 = a.href.replace("data:application/octet;base64,", "")
            .replace("data:image/png;base64,", "")
            .replace("data:image/bmp;base64,", "")
            .replace("data:image/jpeg;base64,", "")
            .replace("data:application/msword;base64,", "")
            .replace("data:application/vnd.ms-excel;base64,", "")
            .replace("data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,", "")
            .replace("data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,", "");
        var binStr = atob(binStr1);
        var buf = new ArrayBuffer(binStr.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i < view.length; i++)
            view[i] = binStr.charCodeAt(i);
        var blob = new Blob([view], { 'type': 'application/octet' });
        var URL = webkitURL.createObjectURL(blob)
        a.href = URL;
        //<=================================================================================================
        if (window.MSBlobBuilder) { // IE10
            var bb = new MSBlobBuilder();
            bb.append(strData);
            return navigator.msSaveBlob(bb, strFileName);
        } /* end if(window.MSBlobBuilder) */

        if ('download' in a) { //FF20, CH19
            a.setAttribute("download", n);
            a.innerHTML = "downloading...";
            D.body.appendChild(a);
            setTimeout(function () {
                try {
                    //a.id = "clickToDownload";
                    //$("#clickToDownload").click();
                    var e = D.createEvent("MouseEvents");
                    e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                    a.dispatchEvent(e);
                    D.body.removeChild(a);
                }
                catch (e) {
                    $.notify("There has been an error trying to download file, please try again or with another browser: " + e.message, "error");
                }
            }, 66);
            return true;
        }; /* end if('download' in a) */

        //do iframe dataURL download: (older W3)
        var f = D.createElement("iframe");
        D.body.appendChild(f);
        f.src = "data:" + (A[2] ? A[2] : "application/octet-stream") + (window.btoa ? ";base64" : "") + "," + (window.btoa ? window.btoa : escape)(strData);
        setTimeout(function () {
            D.body.removeChild(f);
        }, 333);
        return true;
    }