这是我的代码:
var thisImageName = thisRow["imagename"];
var thisImagePath = path.relative("./public", __dirname + "/public/uploads/" + thisImageName + ".jpg");
console.log(thisImagePath); // returns __dirname\public\uploads\
img.src = thisImagePath.split(path.sep).join("/");
Run Code Online (Sandbox Code Playgroud)
要获得适当的图像路径,我必须通过路径分隔符进行拆分,然后使用适当的斜杠连接数组.有谁知道更有效的方法吗?
Eri*_*sDS 14
约翰的答案只会取代'\'的第一个实例
img.src = thisImagePath.replace(new RegExp('\\' + path.sep, 'g'), '/');
Run Code Online (Sandbox Code Playgroud)
将取代所有这些.
您可以传递'g'标志,.replace
但这是非标准的.
jus*_*ase 13
此外,您可以通过专门使用posix路径apis始终在路径中获得正斜杠:
var p = path.posix.relative("./public", imagePath);
Run Code Online (Sandbox Code Playgroud)
编辑:此api仅适用于0.12或更高节点的版本.
归档时间: |
|
查看次数: |
6854 次 |
最近记录: |