在完成上传之前,通过Web套接字切断文件上传

时间:2018-08-01 08:48:19

标签: javascript node.js web

Am使用Web套接字将文件上传到服务器。在我的localhost Web开发中,它正在上传到最后。但是在服务器上,它会在文件上传完成之前中断。

我已经将我的nodejs服务器升级到本地开发的最新版本,并且仍在上传以完成本地开发。但是在服务器上,它总是在完成上传之前被切断。从2MB以上上传文件时,总是会发生这种情况。

服务器代码:

ss(socket).on('upload-data', function (stream, data, ack) {
    jwt.verify(data.token, process.env.JWT_SECRET, function (err, datan) {
        if (err) {
            res.send({
                data: false
            });
        }

        //console.log(data);

        let file_data = fs.createWriteStream(path.resolve(workspace.path + '/' + data.cur_directory, filename));
        stream.pipe(file_data);

        if (data.cur_directory === data.user_directory) {
            let filename = path.basename(data.name);
            //console.log(workspace.path + data.cur_directory + '/' + filename);
            fs.open(workspace.path + '/' + data.cur_directory + '/' + filename, 'wx', function (err) {
                if (err) {
                    ack({ name: filename })
                } else {

                    stream.on('finish', function () {

                        let extname = filename.split('.');
                        extname = extname[extname.length - 1];

                        let filesystem = require('./schema/filesystem.js');

                        let parentPath = data.cur_directory;
                        let filePath = data.cur_directory + '/' + filename;
                        let date = new Date();

                        let year = date.getFullYear();
                        let month = date.getMonth() + 1;
                        let cur_date = date.getDate();
                        let hour = date.getHours();
                        let minutes = date.getMinutes() > 10 ? 0 + date.getMinutes() : date.getMinutes();
                        let seconds = date.getSeconds();

                        let date_generated = year + '-' + month + '-' + cur_date + ' ' + hour + ':' + minutes + ':' + seconds;

                        let file_new_name = path.parse(filePath)

                        let filesystemcontent = new filesystem({ is_dir: false, size: data.size, ext: "." + extname, parent_id: data.current_directory_id, user_id: data.id, name: file_new_name.name, parent: data.cur_directory, parent_path: parentPath, path: filePath, date_created: date_generated });
                        filesystemcontent.save();

                        let docId = filesystemcontent._id;

                        let filesystemcontent2 = require('./schema/filesystem.js');
                        filesystemcontent2.find({ user_id: data.id, parent_id: data.current_directory_id, archive: false }, function (err, doc) {
                            if (err) {
                                return console.log(err);
                            }

                            if (doc.length) {
                                let dir = [];
                                let ds = require('file-system');
                                let filepath = require('path');
                                for (let d = 0; d < doc.length; d++) {
                                    let data = { is_favourite: doc[d].is_favourite, size: doc[d].size, id: doc[d]._id, dir: doc[d].name, dir_stat: ds.lstatSync(workspace.path + '/' + doc[d].path), extname: filepath.extname(workspace.path + '/' + doc[d].path), is_dir: ds.lstatSync(workspace.path + '/' + doc[d].path).isDirectory() }
                                    dir.push(data);
                                }
                                dir.push({ is_favourite: false, size: data.size, id: docId, dir: data.name, dir_stat: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)), extname: filepath.extname(filepath.join(workspace.path + '/' + parentPath, data.name)), is_dir: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)).isDirectory() });

                                ack({ message: 'completed', data: true, found: 0, directory_children: dir });
                            }
                        });

                        //console.log(stream);
                        //console.log(stream._readableState.ReadableState)
                    });

                    stream.on('data', function(data) {
                        //console.log(data)

                    });

                    stream.on('error', function () {
                        stream.end();
                    })
                }
            })
        } else {
            var filename = path.basename(data.name);

            let filesystem2 = require('./schema/filesystem.js');

            filesystem2.find({ user_id: data.id, name: data.cur_directory, _id: data.current_directory_id }, function (err, doc) {
                if (err) {
                    return console.log(err);
                }

                //console.log(doc)

                if (doc.length) {
                    fs.open(workspace.path + '/' + doc[0].path + '/' + filename, 'wx', function (err) {
                        if (err) {
                            ack({ name: filename })
                        } else {
                            let file_data = fs.createWriteStream(workspace.path + '/' + doc[0].path + '/' + filename)
                            stream.pipe(file_data);
                            stream.on('end', function () {

                                let extname = filename.split('.');
                                extname = extname[extname.length - 1];

                                let filesystem = require('./schema/filesystem.js');

                                let parentPath = doc[0].path;
                                let filePath = doc[0].path + '/' + filename;
                                let date = new Date();

                                let year = date.getFullYear();
                                let month = date.getMonth() + 1;
                                let cur_date = date.getDate();
                                let hour = date.getHours();
                                let minutes = date.getMinutes() > 10 ? 0 + date.getMinutes() : date.getMinutes();
                                let seconds = date.getSeconds();

                                let date_generated = year + '-' + month + '-' + cur_date + ' ' + hour + ':' + minutes + ':' + seconds;

                                let file_new_name = path.parse(filePath)

                                let filesystemcontent = new filesystem({ is_dir: false, size: data.size, ext: "." + extname, parent_id: doc[0]._id, user_id: data.id, name: file_new_name.name, parent: data.cur_directory, parent_path: parentPath, path: filePath, date_created: date_generated });
                                filesystemcontent.save();

                                let docId = filesystemcontent._id;

                                let filesystemcontent2 = require('./schema/filesystem.js');
                                filesystemcontent2.find({ user_id: data.id, parent_id: doc[0]._id, archive: false }, function (err, docn) {
                                    if (err) {
                                        return console.log(err);
                                    }

                                    if (docn.length) {
                                        let dir = [];
                                        let ds = require('file-system');
                                        let filepath = require('path');
                                        for (let d = 0; d < docn.length; d++) {
                                            let data = { is_favourite: docn[d].is_favourite, size: docn[d].size, id: docn[d]._id, dir: docn[d].name, dir_stat: ds.lstatSync(workspace.path + '/' + docn[d].path), extname: filepath.extname(workspace.path + '/' + docn[d].path), is_dir: ds.lstatSync(workspace.path + '/' + docn[d].path).isDirectory() }
                                            dir.push(data);
                                        }
                                        dir.push({ is_favourite: false, size: data.size, id: docId, dir: data.name, dir_stat: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)), extname: filepath.extname(filepath.join(workspace.path + '/' + parentPath, data.name)), is_dir: ds.lstatSync(filepath.join(workspace.path + '/' + parentPath, data.name)).isDirectory() })

                                        ack({ message: 'completed', data: true, found: 0, directory_children: dir });
                                    }
                                });
                            })
                        }
                    });
                }
            })
        }

    });
});

客户代码:

var stream = ss.createStream();
ss(socket).emit('upload-data', stream, {name: this.file.name, size: this.file.size, cur_directory: Cookies.get('current_location'), username: Cookies.get('username'), user_directory: Cookies.get('user_directory'), current_directory_id: Cookies.get('current_directory_id'), token: this.$store.state.auth_token, id: Cookies.get('id')}, function(data) {
if(data.name) {
    that.uploading = false;
    that.file_exist = data.name + ' already exist';
}

//console.log(data);
if(data.found === 0) {
    if(data.directory_children.length > 0) {
       that.$store.state.directory_children = data.directory_children;
    }
}
});

ss.createBlobReadStream(this.file).pipe(stream);

0 个答案:

没有答案