0

I'm stuck, trying to get the name of image after uploaded it.

C:\work\assets\pic_items\06c1dd6b-5173-47b6-be09-f5c76866996d.PNG

I always get this result all I want is just the last 06c1dd6b-5173-47b6-be09-f5c76866996d.PNG

I use .split but it doesn't seems to work

picture_path = uploadedFiles[0].fd.z[z.length-1].split('.');
Max Kasem
  • 79
  • 9
  • Possible duplicate of [Get value of a string after a slash in JavaScript](https://stackoverflow.com/questions/8376525/get-value-of-a-string-after-a-slash-in-javascript) – Adeel Imran Dec 27 '17 at 20:21

3 Answers3

0

If you have C:\work\assets\pic_items\06c1dd6b-5173-47b6-be09-f5c76866996d.PNG and want 06c1dd6b-5173-47b6-be09-f5c76866996d.PNG, try:

var fileParts = filePath.split('\\');
filename = fileParts[fileParts.length - 1];
Tom
  • 6,947
  • 7
  • 46
  • 76
0

You'll have to escape the backslashes as they're escape characters themselves:

var str = 'C:\\work\\assets\\pic_items\\06c1dd6b-5173-47b6-be09-f5c76866996d.PNG';
var li = str.lastIndexOf('\\'); // last index of backslash
console.log(str.slice(li + 1)) // 06c1dd6b-5173-47b6-be09-f5c76866996d.PNG
Phix
  • 9,364
  • 4
  • 35
  • 62
0

Try this:

var filePath = 'C:\\work\\assets\\pic_items\\06c1dd6b-5173-47b6-be09-f5c76866996d.PNG';
var fileName = filePath.split('\\').pop();
console.log(fileName) // 06c1dd6b-5173-47b6-be09-f5c76866996d.PNG

This will break the path into parts and then use pop to grab the last entry in the array, which is the filename.

Intervalia
  • 10,248
  • 2
  • 30
  • 60