Generate and save (html/css/js) files directly on the client's browser

Recently, I created a code playground using React similar to liveweave. Users can save their "code playgrounds" and access them later by utilizing Firebase for the database. These "code playgrounds" consist of HTML, CSS, and JavaScript elements. My goal now is to implement a feature that allows users to download their playgrounds in three separate files (one for each language).

1) Is there a way to generate HTML, CSS, and JavaScript files with content on the client-side?

2) If it's possible, could these files be grouped into a .rar file on the client-side as well?

3) If generating files on the client-side isn't feasible or optimal, how would you suggest tackling this issue?

I'm considering creating an Express server to fetch data from the database and then respond with the necessary files, but I'm interested in exploring client-side solutions first.

Answer №1

After weighing my options, I ultimately opted for the fileSaver.js package.

import { saveAs } from "file-saver";
const saveFiles = () => {
    var blob = new Blob([makeHtml(getDocumentCode())], {
      type: "text/plain;charset=utf-8",
    });
    saveAs(blob, `${getFileName()}.html`);
  };

The initial step involves creating a blob from the combined content of the file; in this instance, it consisted of HTML, CSS, and JS code. The makeHtml function is responsible for constructing the HTML document. Subsequently, pass that blob to the saveAs function with the appropriate file name and extension. You can then utilize the saveFiles function in response to various events, such as onClick.

<button onClick={saveFile}>Download<button/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

javascript to enable me to collapse an open accordion

I currently have a side navigation bar with multiple accordions that expand to show more navigation buttons. I have implemented some JavaScript that ensures only one accordion can be expanded at a time. However, I am facing an issue where clicking on the s ...

Increasing the height of list items

I'm currently working on creating a scale using list items, and I want the height of each item to increase incrementally. So far, I haven't been able to find a solution other than adding a class to each item. Here's a fiddle demonstrating t ...

Fade In and Contemplate CSS, Not Effective

Hey there! I'm trying to achieve a similar effect like the one shown in http://designshack.net/tutorialexamples/HoverEffects/Ex5.html. The issue is that the reflection of the image isn't displaying as it should. I've tried inspecting the ele ...

Using Cocoon Gem in Rails 5 to create nested forms within nested forms

Currently, I am experimenting with the cocoon gem to construct nested forms efficiently. Within my application, I have defined models for Organisation, Package::Bip, and Tenor. The relationships between these models are as follows: Organisation has_ma ...

Is hard coding permissions in the frontend considered an effective approach?

I'm in the process of creating an inventory management system that allows admin users to adjust permissions for other employees. Some permissions rely on others to function properly, and I need to display different names for certain permissions on the ...

Categorize an array by shared values

Here is an example of my array structure: myArray = [ {SKU: "Hoo12", Name: "ACyrlic watch",OrderNo:"26423764233456"}, {SKU: "S0002", Name: "Princes Geometry",OrderNo:"124963805662313"}, {SKU ...

"Utilizing jQuery and Bootstrap 4 in TypeScript, attempting to close modal window using jQuery is not functioning

Trying to make use of jquery to close a bootstrap modal within an angular project using typescript code. The following is the code: function call in html: (click)="populaterfpfromsaved(i, createSaved, createProp)" createSaved and createProp are local ...

Is it possible to personalize the Carousel-indicators feature within react-responsive-carousel?

I am currently utilizing the react-responsive-carousel library. I would like to modify the default indicators CSS, changing the dots shape to a line shape. Here is my code snippet: <CarouselWrapper sx={{ marginTop: "124px", maxHeight: & ...

Clickable link in popup window using fancybox

When I try to use fancybox to open an iframe and scroll to an anchor tag, it works perfectly in IE but not consistently in other browsers. It stops at a different place than the anchor. I suspect the issue may be related to JavaScript based on information ...

Receiving numerous requests within the useEffect Hook

In my voting system, users can vote "Yes" or "No", where clicking "Yes" sets the state to up and clicking "No" sets it to down. The voting functionality works well in the voteHandler method, but the issue is that I have to manually refresh the page after e ...

Tips on causing npm to fail when a warning is encountered

I'm currently developing a npm package and am looking for a solution to identify any warnings generated by npm during continuous integration. I've noticed that npm's exit status is typically zero even if warnings are present, as long as ther ...

Message Flash Node.js

Is there a more efficient method for managing req.flash messages? Perhaps creating a function that consolidates all flash messages into one object? app.get('/forgot', function(req, res) { var info = req.flash('info'), error ...

"Unleashing the Glamour: Tips for Decorating an Amazing Ajax Pop

I need help styling a magnific pop-up that displays content from a uri. The content is just plain text and I want to avoid using any html code as the popup will be triggered by a button. The current code I have written functions correctly, but the appeara ...

Ensuring the correct range with HTML TextBoxFor

Is there a way to validate user input in a TextBoxFor to ensure it is less than a certain number at run-time? Here is the current code snippet for reference - <div class="col-md-3 input-group"> <span class="input-group-addon ...

What is the process for saving a file that has been uploaded to a form onto the disk?

The form application extracts data, as well as files, and transmits it in the "formData" format. let formData=new FormData(); formData.append("user",data.user); for(let i=0;i<data.images.length;i++){ formData.append(data.images[i].name, ...

Having trouble with Bing search API not functioning properly with AJAX?

Looking to incorporate Bing's search API using JavaScript. My goal is to allow the user to input a query and retrieve only images from Bing. I attempted to use Ajax for this task. When entering the URL directly in the browser, I successfully retriev ...

Include a carrot icon on a navigation element that is currently active, ensuring it does not disrupt the position of the navigation icon within the container

I am working on a react navigation bar where I want to emphasize each navigation item with a carat when the user is on a specific URL or route. I have been trying to use the :after pseudo-class in Sass to add the carat, but it's not working as expecte ...

jQuery registers the enter event, but does not proceed to trigger the enter action

Hey there, I've been experimenting with jQuery to capture the enter event. Something peculiar is happening though - after pressing enter in the text area, an alert pops up but the text gets entered only after that. Despite trying various solutions, I ...

Prevent the ability to reload and use the F5 key on my PHP page

Currently, I am utilizing a timer on my PHP page. However, whenever someone refreshes the page, the timer resets from the beginning. I am looking for a solution to disable the ability to refresh the page using F5 or reload options within my PHP page. Appr ...

How can I change an array to a string in JavaScript/jQuery and add a specific

Currently, I am tasked with the challenge of converting an array into objects. Furthermore, I also need to add something before each object is displayed. var arrayList = ["image1.jpg","image2.jpg","image3.jpg"]; The desired outcome should look like this ...