0

I have an app where user can upload an svg file. I'm trying to extact the path d information from the uploaded file and save it to my database.

I understand I can inspect element on the svg file and see the path d info, but how can I extract that dynamically?

Ideally I wanna just do an API call where I upload a svg file and it returns path information or something like that, but I can't find anything..

Tried looking up answers in google and stackoverflow but couldnt find how to extract it dynamically, i only found how to find it manually via inspect element

chrwahl
  • 8,675
  • 2
  • 20
  • 30
andrew
  • 15
  • 3
  • You can get the `d` attribute values via [`element.getAttribute('d')`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute). But quite likely you'll first need to parse your file input via `new DOMParser().parseFromString(svgString, "image/svg+xml")`. Please add a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – herrstrietzel Jun 27 '23 at 17:24
  • Thanks @herrstrietzel, I did try to use Python, this is what AI gave me: import xml.etree.ElementTree as ET def get_svg_path('url'): tree = ET.parse(svg_file) root = tree.getroot() path_d = root.attrib['d'] return {"data": path_d} Would this work? I replaced 'url' with the svg file's url but not working so far, telling me to check parameters – andrew Jun 27 '23 at 17:29
  • If you're searching for **python solution** - please add this detail to your title and tags (also include other libraries you're using) – herrstrietzel Jun 27 '23 at 17:35
  • @herrstrietzel im not looking for python solution, i just meant that's what AI suggested me. I think the code you gave me is javascript right? Sorry I'm from low code community and im not too sure what to do with the code. any possibility you can show me how to make that work? i can put it here https://i.imgur.com/yjj5aVY.png – andrew Jun 27 '23 at 17:37
  • Here is a simple [JavaScript example](https://codepen.io/herrstrietzel/pen/VwVpwgm?editors=0010). Pardon me but it's still not clear if you're already using any framework/library (or planning to do so) for your app. Please avoid code screenshots and prefer using Stack overflow's snippet function. – herrstrietzel Jun 27 '23 at 18:15
  • 1
    You have an app where the user can upload an SVG file. What programing language did you use for creating this app? Add that info to your question. – chrwahl Jun 27 '23 at 18:20
  • I'm using bubble.io – andrew Jun 27 '23 at 19:12
  • @andrew: whatever language/framework/library you're using or environment you're working in: It is crucial to **be as specific as possible in your question description**. There are probably bubble.io experts in the SO community who would like to help – **if your question isn't tagged correctly users won't find it**. Besides, posts without any code examples are way more likely to be ignored by SO members – Please **always include code** - whether it works partially or not at all =) – herrstrietzel Jun 28 '23 at 00:10

0 Answers0