Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use jQuery to create a file (and its content) dynamically? [closed]

Here is my HTML code,

<ul>
  <li> Download <a href="#">file1</a> </li>
  <li> Download <a href="#">file2</a> </li>
  <li> Download <a href="#">file3</a> </li>
</ul>

I want to create something like "href=filename.txt". The "filename" of the text file and its content needs to be created dynamically based on which tag was clicked.

I use a web API to get data in JSON format and hence believe security should not be an issue.

like image 947
KAUSHIK H.S. Avatar asked Jan 02 '13 11:01

KAUSHIK H.S.


People also ask

Is jQuery static or dynamic?

The reason to work jquery with static content is they are known to DOM, and can handle the events of known elements. but with dynamic contents you have to bind the event with that element by using .

How can make HTML control dynamically using jQuery?

Create Dynamic Controlsappend() event with the control in which you want to add some dynamic controls and in that append event you need to write the HTML code similar to what you might have written in the body section and this will work for you, for example: <script> $(document). ready(function () {

How can show dynamic data in jQuery?

In this example, we should go with the following steps to implement dynamic data loading using jQuery. Create UI to be appended for each user information. Download and import latest version of jQuery library files and jQuery UI files. Create custom jQuery handlers to append UI and load dynamic data into it.

How does jQuery load work?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.


1 Answers

As of HTML5, you can use a combination of data: URL and download attribute. For example,

<a href="data:text/plain;charset=UTF-8,Hello%20World!" download="filename.txt">Download</a>

Or, programatically,

<a id="programatically" href="#" download="date.txt">Download</a>

$("a#programatically").click(function() {
    var now = new Date().toString();
    this.href = "data:text/plain;charset=UTF-8," + encodeURIComponent(now);
});​

See it here.

Unfortunately, download attribute is not fully supported and data: URLs are in good track.

Now, for a better cross-browser support you will need to create the file dynamically at server-side.

like image 164
Alexander Avatar answered Oct 15 '22 08:10

Alexander