Maker.js export dxf string as a file to user desired location

809 Views Asked by At

I'm using maker.js from microsoft to create svgs and maker.js also allow to export the models to dxf string. I'm able to get dxf string from it. I want to make dxf file from dxf string and save in user desired location or default download location.

Question

How do I create a file and write in it using a string and save it with extension .dxf using javascript.

1

There are 1 best solutions below

0
On

Generally a user has to initiate a download, easiest way I'm aware of is a href to a data url. In React you can render that link as:

<a 
    download="result.dxf"
    href={`data:application/octet-stream;base64,${btoa(dxfString)}`}
>
  Download your DXF
</a>

Where dxfString is a the value returned by makerjs.

Demo of this code in production

HTML only version in jsfiddle

Depending on your framework you'll need a different syntax for changing href of a link. btoa is a standard function in all modern browsers.