【发布时间】:2021-05-10 06:36:48
【问题描述】:
我有一个文件名变量,它返回一个由多个文件名组成的字符串。
我想将所有文件名加入到 1 个数组中。
我尝试了很多方法,但它们都不起作用
这是我的代码:
console.log(filename);
返回:
4ca9da0b-177b-4e8b-ab10-a9633720c1c8getg3434yy.jpg
c721af11-5e6a-43ce-8acc-c9ab86c94c6fhe45yu45uy54y45uj4ju.jpg
3f8e602c-aa64-4c8f-ac5e-ad824bfbfc34h34yh3hyjk5 456j.png
fb05e659-be7a-4f81-8f16-5fb270121b3brjk5ryjkj64iju6i.jpg
我想将它们加入到这样的数组中:
[a950e85b-7cec-487d-9186-38d131d83772g34ygh3435hyu4uu.jpg,
4ca9da0b-177b-4e8b-ab10-a9633720c1c8getg3434yy.jpg,
c721af11-5e6a-43ce-8acc-c9ab86c94c6fhe45yu45uy54y45uj4ju.jpg,
3f8e602c-aa64-4c8f-ac5e-ad824bfbfc34h34yh3hyjk5 456j.png,
fb05e659-be7a-4f81-8f16-5fb270121b3brjk5ryjkj64iju6i.jpg]
我试过了:
let array = [];
for (let index = 0; index <= filename.length; index++) {
const element = filename[index];
array.push(element);
}
console.log(array);
但它会像这样为每个字符串返回:
[
'c56f6f49-197c-46a4-8f33-eb92c7486410g34ygh3435hyu4uu.jpg','c','5','6','f','6','f','4','9','-','1','9','7','c','-','4','6','a','4','-','8','f','3','3','-','e','b','9','2','c','7','4','8','6','4','1','0','g','3','4','y','g','h','3','4','3','5','h','y','u','4','u','u','.','j','p','g',
undefined,
]
[
'c56f6f49-197c-46a4-8f33-eb92c7486410g34ygh3435hyu4uu.jpg','c','5','6','f','6','f','4','9','-','1','9','7','c','-','4','6','a','4','-','8','f','3','3','-','e','b','9','2','c','7','4','8','6','4','1','0','g','3','4','y','g','h','3','4','3','5','h','y','u','4','u','u','.','j','p','g',
undefined
]
[
'14ac4973-cdf8-4657-9075-d81c288a7dc1h34yh3hyjk5 456j.png','1','4','a','c','4','9','7','3','-','c','d','f','8','-','4','6','5','7','-','9','0','7','5','-','d','8','1','c','2','8','8','a','7','d','c','1','h','3','4','y','h','3','h','y','j','k','5',' ','4','5','6','j','.','p','n','g',
undefined
]
[
'6a9214e7-6840-445f-98c8-c40bb660727drjk5ryjkj64iju6i.jpg','6','a','9','2','1','4','e','7','-','6','8','4','0','-','4','4','5','f','-','9','8','c','8','-','c','4','0','b','b','6','6','0','7','2','7','d','r','j','k','5','r','y','j','k','j','6','4','i','j','u','6','i','.','j','p','g',
undefined
]
[
'1534bb5c-6ab1-4d26-a9d0-6ec73b377272getg3434yy.jpg','1','5','3','4','b','b','5','c','-','6','a','b','1','-','4','d','2','6','-','a','9','d','0','-','6','e','c','7','3','b','3','7','7','2','7','2','g','e','t','g','3','4','3','4','y','y','.','j','p','g',
undefined
]
[
'c090778c-feab-4c4d-925a-462d289cbf4ehe45yu45uy54y45uj4ju.jpg','c','0','9','0','7','7','8','c','-','f','e','a','b','-','4','c','4','d','-','9','2','5','a','-','4','6','2','d','2','8','9','c','b','f','4','e','h','e','4','5','y','u','4','5','u','y','5','4','y','4','5','u','j','4','j','u','.','j','p','g',
undefined
]
感谢回答
编辑:完整代码
import { Request, Response } from "express";
import { File } from "formidable";
import Formidable from "formidable-serverless";
import fs from "fs";
import { v4 as uuidv4 } from "uuid";
export const config = {
api: {
bodyParser: false,
},
};
export default function uploadFormFiles(req: Request, res: Response) {
return new Promise(async (resolve, reject) => {
const form = new Formidable.IncomingForm({
multiples: true,
keepExtensions: true,
});
form
.on("file", (name: string, file: File) => {
const filename = uuidv4() + file.name;
let array = [];
for (let index = 0; index <= filename.length; index++) {
const element = filename[index];
array.push(element);
}
console.log(array);
const data = fs.readFileSync(file.path);
fs.writeFileSync(`public/upload/${filename}`, data);
fs.unlinkSync(file.path);
})
.on("aborted", () => {
reject(res.status(500).send("Aborted"));
})
.on("end", () => {
resolve(res.status(200).send("done"));
});
await form.parse(req);
});
}
编辑 2:解决方案: 文件名的生成真的很奇怪......它是从 TypeScript 生成的
const fields = new Formidable.IncomingForm({
multiples: true,
keepExtensions: false,
});
fields.parse(req, (err, fields, files) => {
let filename = files.file.map((a) => uuidv4() + a.name);
}
【问题讨论】:
-
告诉我们
filee是什么。似乎您在第二次迭代时迭代了一个字符串,这会导致您看到的输出乱码。 -
如何获取所有文件名?
-
@Terry 对不起,filee 应该是文件名
-
我从我的项目代码中复制了它,所以我忘了更改它,更新了包含完整详细信息的代码
-
@Reyno 我从具有文件输入字段的表单中获取它,用户可以选择多个文件并将它们上传到服务器一切正常的文件,但我无法将文件名加入到数组中跨度>
标签: javascript arrays string