“文件”事件中的男生未触发

时间:2018-07-10 16:30:21

标签: javascript node.js busboy

这个问题已经被提出,但是没有找到解决方案。我正在使用busboy上传文件,但是由于我有以下代码,因此它不会触发文件事件:

用户界面端

 startUpload(): void {
   this.fileToUpload = this.files[0].nativeFile;// file from ui
        const event: UploadInput = {
            type: 'uploadAll',
            url: AppSettings.API_ENDPOINT + 'upload/blogFileUpload/',
            method: 'POST',
            data: { uploadfile: this.fileToUpload }
        };
        this.uploadInput.emit(event);
    }

nodejs

function upload(req, callback) {
    var fileRoute = config.file.blogdir;
    var saveToPath = null;
    var hadStreamError = null;
    var link = null;
    function handleStreamError(error) {

        if (hadStreamError) {
            return;
        }

        hadStreamError = error;

        if (saveToPath) {
            return fs.unlink(saveToPath, function (err) {
                return callback(error);
            });
        }
        return callback(error);
    }

    try {
        var busboy = new Busboy({
            headers: req.headers
        });

    } catch (e) {
        return callback(e);
    }

    // Handle file arrival.
    busboy.on("file", function (fieldname,file,filename, encoding, mimetype) {

        if ("file" != fieldname) {
            // Stop receiving from this stream.
            file.resume();
            return callback("Fieldname is not correct. It must be " + file + ".");
        }

        var randomName = sha1(new Date().getTime()) + "." + getExtension(filename);
        link = fileRoute + randomName;
        var appDir = path.dirname(require.main.filename);
        saveToPath = path.join(appDir, link);
        file.on("error", handleStreamError);
        var diskWriterStream = fs.createWriteStream(saveToPath);
        diskWriterStream.on("error", handleStreamError);
        diskWriterStream.on("finish", function () {
            var status = isImageValid(saveToPath, mimetype);
            if (!status) {
                return handleStreamError("File does not meet the validation.");
            }

            return callback(null, {
                link: link
            });
        });
        console.log("diskWriterStream",diskWriterStream);
        // Save image to disk.
        file.pipe(diskWriterStream);
    });

    busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
        console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    });

    busboy.on('finish', function () {
        console.log("out of busboy");
        // res.sendStatus(200);
    });

    busboy.on("error", handleStreamError);
    req.on("error", handleStreamError);
    return req.pipe(busboy);

}

使用froala-editor通过busboy上载博客内容来上传blob内容问题是,当我将文件上载到编辑器时,它不会触发文件事件,因此文件未上载。 预先谢谢你,每个建议都会对我有很大帮助。

0 个答案:

没有答案