简体   繁体   中英

Rename file on dropzone.js with user ID on meteor app

I have a meteor application with the plugin dropzone.js . I want to save the images from the dropzone with the ID of the current user and the file name. But I don't know how to add the user ID in front of the file name. I can rename the file with

    getFileName: function(file, formData) {
        return 'some text' + file.name;
    }

My server file is :

Meteor.startup(function () {
    UploadServer.init({
    tmpDir: process.env.PWD + '/public/uploads',
    uploadDir: process.env.PWD + '/public/uploads',
    checkCreateDirectories: true,
    uploadUrl: '/upload',
    // *** For renaming files on server
    getFileName: function(file, formData) {
        return file.fileName + file.name;
    }
  });
});

But, if I try to get the user ID with Meteor.userId() or this.userId, this doesn't work because I'm in a server side file. So, do you know how I could pass the user ID on a server side file ?

Import the meteor object into the file where you want the userId and then just use Meteor.userId() . Never pass the userId from the client.

import { Meteor } from 'meteor/meteor';

Meteor.startup(function () {
  UploadServer.init({
    tmpDir: process.env.PWD + '/public/uploads',
    uploadDir: process.env.PWD + '/public/uploads',
    checkCreateDirectories: true,
    uploadUrl: '/upload',

    // *** For renaming files on server
    getFileName: function(file, formData) {
        return Meteor.userId() + file.fileName;
    }
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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