0

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

   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);
4

1 回答 1

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);

    });
于 2017-08-02T08:00:27.900 回答