Download as Text file in Salesforce Lightning Web Component
Feb 21, 2025
We can download the content as text file in Salesforce Lightning Web Component by creating an anchor element and making use of the click() method.
Blog Post:
https://www.infallibletechie.com/2025/02/download-as-text-file-in-salesforce-lightning-web-component.html
Show More Show Less View Video Transcript
0:05
hello everyone in this video we are
0:07
going to see how to
0:10
download content as text file in
0:13
Salesforce lightning web component let's
0:16
dive into the Demo
0:18
First here I'm going to enter something
0:22
uh dummy
0:26
information this is a lightning text
0:28
area input
0:31
field I have download icon once I click
0:34
it it will download
0:37
it so once I click this download I'm
0:40
able to download it and this is the
0:46
content let's see how I was able to
0:49
achieve it and let's see the code behind
0:53
it this is the light lightning web
0:55
component HTML using lightning button
0:58
icon I'm displaying
1:01
um a button icon so that uh once the
1:04
users click it it will uh download it
1:08
this is not
1:11
needed and
1:14
uh I'm making use of lightning text area
1:17
tag so that uh it will display an input
1:20
field for text area when whenever they
1:23
click enter it will go to the next line
1:25
and then they will be able to enter
1:27
multiple uh uh lines of information
1:36
here whenever they change the
1:38
information on the text
1:40
area I'm calling this
1:44
method and then I'm storing the entered
1:47
information in St Str message
1:50
variable whenever they
1:53
click download
1:56
conversation the download icon it will
1:59
will invoke handle download
2:02
method in the handle download method I
2:05
am calling download string
2:08
method here I am passing the information
2:11
that was
2:12
entered and then uh I am passing the
2:15
file type as
2:17
text/plain and I'm entering the file
2:20
name as testing. txt so in this method
2:24
I'm first creating a blob using the
2:27
content and I'm making use of the type
2:30
as the file type so here it will be
2:33
text/plain text as the file type I'm
2:36
creating an anchor tag using document.
2:39
Create element I'm setting the download
2:41
as the file name for uh reference I'm
2:46
making use of url. create object URL of
2:49
The Blob information the entire
2:51
information using for the data set.
2:54
download URL I'm passing the file type
2:58
the download information which will be
3:01
the file name and the reference
3:04
information I'm joining everything
3:08
using
3:10
colon I don't want to display the anchor
3:13
tag so I'm setting the style as none and
3:17
I'm making use of the click
3:19
method once it clicks it will download
3:22
the file and I'm setting after a um 1
3:26
second Gap using url. r object URL
3:31
method I'm revoking that URL so that uh
3:36
there won't be any conflict and uh I
3:38
don't want to have that in the reference
3:40
so I'm just removing it using the url.
3:44
revoke object URL
3:46
method please check the video
3:48
description in the video description I
3:50
have given my blog post from the blog
3:52
post you should be able to get this
3:54
sample lightning web component code for
3:57
your reference
4:04
I hope it was
4:11
helpful thank you for watching
#Internet Software
#File Sharing & Hosting
#Clip Art & Animated GIFs
