Jump to content

In HTML I need example code on how to save text box input to a .txt file

snozking

In HTML just a text box with an enter button and then it saves the textbox to a local file.

 

Ive tried to do this but nothing I found works and I dont understand much. I only know you need javascript.

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, snozking said:

In HTML just a text box with an enter button and then it saves the textbox to a local file.

 

Ive tried to do this but nothing I found works and I dont understand much. I only know you need javascript.

I think for security reasons you can't generate local files programmatically in JS.

 

The way I see to do it, you'd need a web server that receives the content of the box and serves it back as a file.

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | a 10G NIC (pending) | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4× WD 10TB / 4× Seagate 14TB Exos / 8× WD 12TB (custom external SAS enclosure) / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X550-T2 10G NIC | LSI 9300-16i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, AbydosOne said:

I think for security reasons you can't generate local files programmatically in JS.

 

The way I see to do it, you'd need a web server that receives the content of the box and serves it back as a file.

sad

Link to comment
Share on other sites

Link to post
Share on other sites

Is this an example of the X-Y problem?

____________________________________________________________________________________________________________________________________

 

 

____________________________________________________________________________________________________________________________________

pythonmegapixel

into tech, public transport and architecture // amateur programmer // youtuber // beginner photographer

Thanks for reading all this by the way!

By the way, my desktop is a docked laptop. Get over it, No seriously, I have an exterrnal monitor, keyboard, mouse, headset, ethernet and cooling fans all connected. Using it feels no different to a desktop, it works for several hours if the power goes out, and disconnecting just a few cables gives me something I can take on the go. There's enough power for all games I play and it even copes with basic (and some not-so-basic) video editing. Give it a go - you might just love it.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, pythonmegapixel said:

Is this an example of the X-Y problem?

maybe but idk. i know what i need but i dont know how to implement it

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, snozking said:

i know what i need

"Why do you need it?" is what the X-Y Problem is trying to get at. Maybe the reason you need this specific application has already been solved in a more general sense.

Main System (Byarlant): Ryzen 7 5800X | Asus B550-Creator ProArt | EK 240mm Basic AIO | 16GB G.Skill DDR4 3200MT/s CAS-14 | XFX Speedster SWFT 210 RX 6600 | Samsung 990 PRO 2TB / Samsung 960 PRO 512GB / 4× Crucial MX500 2TB (RAID-0) | Corsair RM750X | a 10G NIC (pending) | Inateck USB 3.0 Card | Hyte Y60 Case | Dell U3415W Monitor | Keychron K4 Brown (white backlight)

 

Laptop (Narrative): Lenovo Flex 5 81X20005US | Ryzen 5 4500U | 16GB RAM (soldered) | Vega 6 Graphics | SKHynix P31 1TB NVMe SSD | Intel AX200 Wifi (all-around awesome machine)

 

Proxmox Server (Veda): Ryzen 7 3800XT | AsRock Rack X470D4U | Corsair H80i v2 | 64GB Micron DDR4 ECC 3200MT/s | 4× WD 10TB / 4× Seagate 14TB Exos / 8× WD 12TB (custom external SAS enclosure) / 2× Samsung PM963a 960GB SSD | Seasonic Prime Fanless 500W | Intel X550-T2 10G NIC | LSI 9300-16i HBA | Fractal Design Node 804 Case (side panels swapped to show off drives) | VMs: TrueNAS Scale; Ubuntu Server (PiHole/PiVPN/NGINX?); Windows 10 Pro; Ubuntu Server (Apache/MySQL)


Media Center/Video Capture (Jesta Cannon): Ryzen 5 1600X | ASRock B450M Pro4 R2.0 | Noctua NH-L12S | 16GB Crucial DDR4 3200MT/s CAS-22 | EVGA GTX750Ti SC | UMIS NVMe SSD 256GB / TEAMGROUP MS30 1TB | Corsair CX450M | Viewcast Osprey 260e Video Capture | Mellanox ConnectX-2 10G NIC | LG UH12NS30 BD-ROM | Silverstone Sugo SG-11 Case | Sony XR65A80K

 

Camera: Sony ɑ7II w/ Meike Grip | Sony SEL24240 | Samyang 35mm ƒ/2.8 | Sony SEL50F18F | Sony SEL2870 (kit lens) | PNY Elite Perfomance 512GB SDXC card

 

Network:

Spoiler
                           ┌─────────────── Office/Rack ────────────────────────────────────────────────────────────────────────────┐
Google Fiber Webpass ────── UniFi Security Gateway ─── UniFi Switch 8-60W ─┬─ UniFi Switch Flex XG ═╦═ Veda (Proxmox Virtual Switch)
(500Mbps↑/500Mbps↓)                             UniFi CloudKey Gen2 (PoE) ─┴─ Veda (IPMI)           ╠═ Veda-NAS (HW Passthrough NIC)
╔═══════════════════════════════════════════════════════════════════════════════════════════════════╩═ Narrative (Asus USB 2.5G NIC)
║ ┌────── Closet ──────┐   ┌─────────────── Bedroom ──────────────────────────────────────────────────────┐
╚═ UniFi Switch Flex XG ═╤═ UniFi Switch Flex XG ═╦═ Byarlant
   (PoE)                 │                        ╠═ Narrative (Cable Matters USB-PD 2.5G Ethernet Dongle)
                         │                        ╚═ Jesta Cannon*
                         │ ┌─────────────── Media Center ──────────────────────────────────┐
Notes:                   └─ UniFi Switch 8 ─────────┬─ UniFi Access Point nanoHD (PoE)
═══ is Multi-Gigabit                                ├─ Sony Playstation 4 
─── is Gigabit                                      ├─ Pioneer VSX-S520
* = cable passed to Bedroom from Media Center       ├─ Sony XR65A80K (Google TV)
** = cable passed from Media Center to Bedroom      └─ Work Laptop** (Startech USB-PD Dock)

Retired/Other:

Spoiler

Laptop (Rozen-Zulu): Sony VAIO VPCF13WFX | Core i7-740QM | 8GB Patriot DDR3 | GT 425M | Samsung 850EVO 250GB SSD | Blu-ray Drive | Intel 7260 Wifi (lived a good life, retired with honor)

Testbed/Old Desktop (Kshatriya): Xeon X5470 @ 4.0GHz | ZALMAN CNPS9500 | Gigabyte EP45-UD3L | 8GB Nanya DDR2 400MHz | XFX HD6870 DD | OCZ Vertex 3 Max-IOPS 120GB | Corsair CX430M | HooToo USB 3.0 PCIe Card | Osprey 230 Video Capture | NZXT H230 Case

TrueNAS Server (La Vie en Rose): Xeon E3-1241v3 | Supermicro X10SLL-F | Corsair H60 | 32GB Micron DDR3L ECC 1600MHz | 1x Kingston 16GB SSD / Crucial MX500 500GB

Link to comment
Share on other sites

Link to post
Share on other sites

48 minutes ago, snozking said:

In HTML just a text box with an enter button and then it saves the textbox to a local file.

Impossible. Web pages don't have access to the local file system for fairly obvious reasons. You can do this if you have a backend.

 

Or, depending on what you need, you could write a native program or at least something that runs outside the browser that does it.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Sauron said:

Impossible. Web pages don't have access to the local file system for fairly obvious reasons. You can do this if you have a backend.

 

Or, depending on what you need, you could write a native program or at least something that runs outside the browser that does it.

Not the op but could you do it so that it brings up the "download file" dialogue without backend?

Websites can download a file to my pc.

I only see your reply if you @ me.

This reply/comment was generated by AI.

Link to comment
Share on other sites

Link to post
Share on other sites

You could probably encode the text as base64 string and put it in the url field of a "Download" button and tell user to right click and say save as or something ...

 

ex 

data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==

<p><a href="data:text/plain ... "> Download </a> document </p>

 

The above base64 encoding is for "Hello, World" as shown here : https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

 

There's javascript function to convert to base64 and you can change the url href with javascript. Don't know if you can force click on the button through javascript as if user clicks on it.

 

the same data encoding scheme is used to embed small icons / images in html pages without loading different resources.

 

note there's some limitations about maximum url lengths , some browsers limit to 65k bytes so you'd be limited to around 20-30K of text, other browsers may limit you to 10-16k  so about half of that as actual content after converting back from base64

 

edit : maybe see if adding the "download" keyword will trigger the download/save as window ... it's something relatively new

 

<a href=" url"   download > Download me </a> 

 

See  https://www.webfx.com/blog/web-design/download-attribute/

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Of course it's possible. This should work

<!-- HTML -->
<a id="link" target="_blank" download="file.txt">Download</a>

 

// only client side JS
var data = ["Hello world!"];
var file = new File(data, "file.txt", {type: 'text/plain'});
var url = URL.createObjectURL(file);
document.getElementById('link').href = url;

Live CodePen example

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Origami Cactus said:

Not the op but could you do it so that it brings up the "download file" dialogue without backend?

sure, but that's not what was asked. it may be an acceptable solution depending on what the actual requirement is. if user input is acceptable then I wonder why they wouldn't just use notepad.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Hm, I've handled multiple GB sized Zip files with blazor wasm and from my experience the solution of @shadow_ray should be the most performant.
And to have a little more complete example:


html:

<textarea id="textInput"></textarea><br>
<button onclick="downloadText('text.txt')">Download</a>

js:

const textInput = document.getElementById('textInput');

function downloadText(fileName) {
  const data = [textInput.value];
  const file = new File(data, fileName, {type: 'text/plain'});
  const url = URL.createObjectURL(file);
  triggerDownload(fileName, url);
  URL.revokeObjectURL(url);
}

function triggerDownload(fileName, url) {
  const anchorElement = document.createElement('a');
  anchorElement.href = url;
  anchorElement.download = fileName ?? '';
  anchorElement.click();
  anchorElement.remove();
}

Software developer.
Case: be quiet! Dark Base Pro 900 Orange rev. 2 MB: GIGABYTE Z590 AORUS ULTRA (rev. 1.0) CPU: Intel® Core™ i9-11900K WC: be quiet! Pure Loop 280mm GPU: AORUS GeForce RTX 3080 Ti MASTER 12G RAM: 128GB (4x Kingston FURY 32 GB DDR4-3600) M.2: 3TB (Samsung 970 EVO Plus 1 TB & Crucial P5 2TB PCIe M.2 2280SS) PSU: be quiet! Dark Power Pro 12 1500W

Link to comment
Share on other sites

Link to post
Share on other sites

  • 1 month later...
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <textarea oninput="document.querySelector('a').href = 'data:text/plain;base64,' + btoa(this.value)"></textarea>
  <a href="data:text/plain;base64," download="text.txt"><button>enter</button></a>
</body>

</html>

here ya go. it's one line of javascript

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×