我想从 Angular ReactivForms 中图像的 FormControl 获取名称。我获得了图像的完整路径,例如 file = "C:\fakepath\Screenshot (1).png"
但我只想要 Screenshot (1).png
。
我使用 control.value
来获取图像的名称。
我想添加我的 C: 目录中没有此文件夹 C:\fakepath\
这是我的方法:
requiredFileType(categoryList: CategoryModel[]) {
return function (control: FormControl) {
const file = control.value;
if (file) {
const extension = file;
for (let i = 0; i < categoryList.length; i++) {
for (let j = 0; j < categoryList[i].productModel.length; j++) {
for (let k = 0; k < categoryList[i].productModel[j].imageProduct.length; k++) {
console.log(categoryList[i].productModel[j].imageProduct[k].image_path);
if (categoryList[i].productModel[j].imageProduct[k].image_path.includes(extension)) {
console.log(categoryList[i].productModel[j].imageProduct[k].image_path);
return{
requiredFileType: true
};
}
}
}
}
return null;
}
return null;
};
}
有谁知道这个问题的解决办法吗?
最佳答案
如果您的路径具有静态字符数(不变),请使用substring
:
const file = control.value;
const fileName = file.substring(10, str.length);
演示:
let str = "C:\fakepath\Screenshot (1).png";
const mystr = str.substring(10, str.length);
console.log(mystr)
关于angular - 如何从 ReactivForms 中的 FormControl 返回图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56691376/