I have tested your code and it works in Lightning Experience (LEX) from VF page. The only thing, which doesn't work - when I click on button it initiates downloading csv file named download.csv in Chrome or randomly generated name in Firefox (not mydata.csv as you specified in JS).
Reason why it doesn't works in LEX? In the Salesforce app and Lightning Experience, Visualforce runs inside an iframe that’s inside the larger /lightning container. I guess because of that browsers ignore name specified in download attribute.
Also, I have tried your code directly in JS of lightning component/app. And, everything works as expected.
As a workaround, you can create for LEX separate button. And here you have two options:
- You can create lightning app with the same JS code on button click. User should be redirected to your lightning app /c/YourAppName.app instead of VF page.
- You can create lightning component which implements
force:lightningQuickAction
with the same JS code on button click.