如何在上传前更改dropzone中的File.Name

时间:2017-08-01 06:43:07

标签: javascript reactjs superagent react-dropzone

在下面的代码中,我有两个控制器,即上传/发布和文档/帖子,由于秒,它在API上传输时保存file.name的难度很小,有时两者不匹配,例如数据库一file20170801053331.jpg,上传的图片将是file20170801053332.jpg,因为这两个不匹配所以我打算将文件名传输到上传控制器,所以它将是相同的文件.name无法更改。

   const req = request.post('http://localhost:8933/api/upload/post');
            acceptedFiles.forEach(file => {


                var now = new Date().toLocaleString('en-PH');
                form.State = 1;
                var cdate =dateFormat(now, "yyyymmddHHMMs")
                var ext = file.name.split(".").slice(-1)[0];
                var ext2 = file.name.split("."+ext).slice(0)[0];
                var fileNames = (ext2+cdate+"." +ext);
                form.Link=fileNames;
                form.Name=file.name;
                file.name = fileNames; // this is what im trying to do but it wasnt working  
                req.attach(file.name, file);
                this.props.post('document/post', form);
            });
                req.end(callback);

1 个答案:

答案 0 :(得分:0)

您可以通过在req.attach上添加新名称来更改file.name

    var name =  form.something.length;
    var acceptedFiles = form.passFile;
    const req = request.post('http://localhost:8933/api/upload/post');
    acceptedFiles.forEach(file => {
        form.State = 1;
        var cdate =uuidv1();
        var ext = file.name.split(".").slice(-1)[0];
        var ext2 = file.name.split("."+ext).slice(0)[0];
        var fileNames = (ext2+cdate+"." +ext); // call this and put it on the req.attach
        form.Link=fileNames;
        form.Name=file.name;
        req.attach(file.name, file, fileNames);
        this.props.post('document/post', form);

    });