How to save a text as .txt file via a downloadable link? - html

HTML newbie here
I am working on an app using Streamlit. Based on the user inputs to the fields available, I am generating some data which I want to download in the form of a .txt file.
The data that I want to download is generated when I do
to_save = abc.serialize().encode("ascii", "ignore")
and when I do print(to_save), I get (this is just a small part of a very huge text data)
b"UNA:+.?
'UNB+UNOC:3+9978715000006:14+9978715000006:14+200529:1139+50582307060_WP?+_200101_200201++TL'UNH+1+MSCONS:D:04B:UN:2.3'BGM+7+50582307060_WP?+_200101_200201-1+9'DTM+137:202005291139:203'RFF+Z13:13008'NAD+MS+9978715000006::9'CTA+IC+:Michael
Jordan'COM+m.jordan#energycortex.com:EM'NAD+MR+9978715000006::9'"
Now, I want to save this information as a .txt file via an HTML link. I am following:
How to download a file in Streamlit
How to force fully download txt file on link?
and I have
reference = 50582307060_WP+_200101_200201
to_save = abc.serialize().encode("ascii", "ignore")
href = f'<a href="data:text/plain;charset=UTF-8,{to_save}" download={reference}.txt>Download File</a> (right-click and save as {reference}.txt)'
st.markdown(href, unsafe_allow_html=True)
But this doesn't work and shows as follows:
The start
The end
and when I do:
to_save = abc.serialize().encode("ascii", "ignore")
href = f'<a href="data:text/plain;charset=UTF-8" download={reference}.txt>Download File</a> (right-click and save as {reference}.txt)'
st.markdown(href, unsafe_allow_html=True)
I get
The problem with this being that the information that has to be saved as a .txt file (to_save = abc.serialize().encode("ascii", "ignore")) isn't being saved and I get a Failed-Network error
What is the mistake that I am doing and how can I enable saving the information stored in to_save (to_save = abc.serialize().encode("ascii", "ignore")) as an HTML downloadable link? Also, the file should be saved as 'reference.txt', with reference being defined as a variable above.

I reckon I have found a solution to your problem. Though I can't be entirely sure, it has two causes. The first one lies in the href attribute of the download link. The problem here is the " (double quotes) in the to_save variables data. The html, for what I could test with the by you provided data, renders it as follows:
<a href="data:text/plain;charset=UTF-8,b"UNA:+.? 'UNB+UNOC:3+9978715000006:14+9978715000006:14+200529:1139+50582307060_WP?+_200101_200201++TL'UNH+1+MSCONS:D:04B:UN:2.3'BGM+7+50582307060_WP?+_200101_200201-1+9'DTM+137:202005291139:203'RFF+Z13:13008'NAD+MS+9978715000006::9'CTA+IC+:Michael Jordan'COM+m.jordan#energycortex.com:EM'NAD+MR+9978715000006::9'"" download=filename.txt>Download File</a>
As you can see the value of the href attribute isn't all in the blue color (here in the stackoverflow code container above). That is because of the " that interrupt the string, it closes the earlier opened " after href=". In order to prevent this behaviour, you should replace the " in to_save with ". To the user this will look the same as " but the browser will treat it like a normal string.
You should add the following line of code to your python script to make that happen
to_save = abc.serialize().encode("ascii", "ignore")
#add this line:
to_save = to_save.replace('"','"')
Next the download attribute doesn't have any double quotes around it's value. It should, formally, look like this: download="filename.txt". Then again, for safety replace any possible"with"`.
The full python code should now look like this:
reference = 50582307060_WP+_200101_200201
reference = reference.replace('"','"')
to_save = abc.serialize().encode("ascii", "ignore")
to_save = to_save.replace('"','"')
href = f'Download File (right-click and save as {reference}.txt)'
st.markdown(href, unsafe_allow_html=True)
Hope this helps! If not, please comment.

Related

Save checkbox input in offline HTML

I am working on a project in R where I export a resulting dataset into HTML format that is saved on dropbox or goole drive, then the team members have to review each particular row and mark it if it meets criteria. Each member will be working on their own copy of the HTML file.
I have very little experience with HTML files, but I was able to generate an output using the r code below. This results has a column with a check box for each row. The problem is that it does not save the user's choice, ie when I refresh or reopen the file, I do not see the choice I already made. I understand that this likely will need a more complex html coding (if even doable without placing this html on a website).
I thought about just exporting into pdf or something and the user can save after review, but I still want to try with HTML as the resulting document can be viewed without any pdf reader installed and will fit the width to whatever device / screen size used.
was hoping I can find some guidance on where to go from there.
Thank you
library(tableHTML)
name = c("wiki", "google")
link = c("https://en.wikipedia.org/wiki/Main_Page", "https://www.google.com/")
df = data.frame(name, link)
df$link2 = paste('', df$name, '', sep="")
df$check = '<input type="checkbox" id="v1" name="v1" value="checked">
<label for="vehicle1"> Mark</label>'
df$link = NULL
tableHTML::write_tableHTML(tableHTML(df, escape = F), file = "df.html")

vb.net how to add and load an html file from resource into webbrowser control

I have a webbrowser control on form2, and the things I'd like to do are:
How can I add an HTML file into resource ?
How can I load it from resource to webbrowser control when an if-condition is true, and make it visible in the webbrowser?
For example:
If dttest.Rows(0).Item(0).ToString = TextBox1.Text Then
webbrowser.navigate(resources and the html name page)
End If
I've already tried the webbrowser.navigate but I want to load the HTML page.
This work's for me and is tried and tested... I left the If condition out as it's not pertinent of what you are trying to accomplish. Also no need to call Navigate as when you set the Uri it should load the document.
Dim filename As String = "test.html"
With Me.WebBrowser1 'Change name to reflect your name
.ScriptErrorsSuppressed = True 'I was getting script errors from site
.Url = New Uri(String.Format("file:///{0}{1}{2}/", System.IO.Directory.GetCurrentDirectory(), "/Resources/", filename))
End With
Note
If the Copy to Output Directory is not set to Copy Always for your html file (file's) you need to make that change. You can right click on the html file and select properties. Change that property as needed.

Save pictures in document using Powershell and Word 2013

I'm trying to convert a HTML file with externally linked images to RTF using MS Word 2013 via Powershell commands, when using below command file gets converted however pictures are missing
$wrd = New-Object -ComObject "Word.Application"
$doc = $wrd.Documents.Open('c:\test.html')
$opt = [ref][Microsoft.Office.Interop.Word.WdSaveFormat]::WdFormatRTF
$name= [ref]'C:\test.rtf'
$wrd.ActiveDocument.SaveAs($name, $opt)
$wrd.ActiveDocument.Close()
$wrd.Quit()
If I do the conversion manually by opening the HTML file with Word and saving it as RTF same thing happens, no images, however if within Word I go to File, click on "Edit Links to File" and highlight all images, tick "Save picture in document" and then click on "Break Link" and then do the save as RTF, this time images are present within RTF (however with bad quality which is another issue..) [See picture below]
Is there a way to execute above process within Powershell?
Thanks
This does the job
$images = $doc.InlineShapes
foreach ($image in $images) {
$linkFormat = $image.LinkFormat
$linkFormat.SavePictureWithDocument = 1
$linkFormat.BreakLink()
}

LOCAL HTML file to generate a text file

I am trying to generate a TEXT/XML file from a LOCAL HTML file. I know there are a lot of answers to generating a file locally, usually suggesting using ActiveX object or HTML 5.
I'm guessing there is a way to make it work on all browsers (in the end HTML extension is opened by a browser even if it is a LOCAL file) and easily since this is a LOCAL file put in by user himself.
My HTML file will be on client's local machine not accessed via HTTP.
It is basically just a form written in HTML that upon "SAVE" command should be generating an XML file in the local disk (anywhere user decides) and saving form's content in.
Any good way?
One way that I can think of is, the html form elements can be set into class variables and then using the jaxb context you can create an XML file out of it.
Useful Link: http://www.vogella.com/tutorials/JAXB/article.html
What you can do is use base64 data-urls (no support for IE9-) to download the file:
First you need to create a temporary iframe element for your file to download in:
var ifrm = document.createElement('iframe');
ifrm.style.display = 'none';
document.body.appendChild(ifrm);
Then you need to define what you want the contents of the file to download to be, and convert it to a base64 data-url:
var html = '<!DOCTYPE html><html><head><title>Foo</title></head><body>Hello World</body></html>';
htmlurl = btoa(html);
and set it as source for the iframe
ifrm.src = 'data:text/x-html;base64,'+htmlurl;

how to set default location in browse tag

Am doing project in j2ee.I use Browse button. I need to fetch images/files from a folder inside my project folder, everytime.I wish to set my project's folder name as a default location for my browse button.How to do that?
am using tis following code in a jsp page:
and calling the value in servlet :
String image= (String) request.getParameter("img");
while browsing ,the whole path is getting displayed but only the name of the image is passing as value in the sevlet.How to get the entire path? or how to set my project folder as default location while browsing?
If this is rendering a standard
<input type="file".../>
Then you have no control over the default directory.
Using flash gains you a bit more control (filetype preset, single/multiple file selection etc.)
I'm not sure if Java Applets have more access/options, hopefully someone can advise.
PS. for the standard input tag, the browser will remember the last location you used, so you only need to navigate once, if you are uploading several files from the same directory.
I actually solved this problem with an interesting trick. Here's how...
Create an external vbscript include file called vbshelper.inc which contains the following code:
function stuffKeys(x)
Set wshShell = CreateObject("WScript.Shell")
wshShell.Sendkeys(x)
end function
Inside your HTML code header, place the following line as your first < Script> element...
< Script type="text/VBScript src="vbshelper.inc">
later, in your HTML code, create the button like this...
< input type="File" id="srcFile" onchange="go_do_something()" onclick="stuffkeys(' C:\Temp\*.txt~')">
Note the space before the C in ' C:\Temp*.txt~' and the tilde ~ after the .txt.