0

I am trying to download some csv content via button click.

On click i have a javascript function that downloads the file using

window.location='data:application/webcsv;charset=utf8,' + encodeURIComponent(csvData);

But this file does not have any name and extension. How can i assign a file name using javascript?

sharmacal
  • 457
  • 1
  • 6
  • 25

2 Answers2

0

There's no way to download a file with a specified filename using pure JS that I know of - a combination of JS and Flash can be used though.

Check out the Downloadify library - I used it to do just this same thing recently...

Karl Barker
  • 11,095
  • 3
  • 21
  • 26
0

You can't specify filename or extension via the data uri. But if you use an <a> element, you can use the [download] attribute:

<a href="data:..set dynamically.." download="filename.extension">Download</a>

It's not supported in every browser, but it's a start.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • thanks. this worked out for me. inside the function i changed the href of an anchor tag which onclick downloaded the file with proper extension. – sharmacal Feb 01 '13 at 16:07
  • can u suggest a solution for mobile browsers too. – sharmacal Feb 01 '13 at 19:56
  • @sharmacal, if I knew one, I'd suggest it. Unfortunately, `[download]` is a little too new to be widely supported. I use it on a custom template builder for myself, but that's because I know I'm going to use chrome. – zzzzBov Feb 01 '13 at 20:02