Saving URL path in the text file by using JavaScript - javascript

I would like to save/export my URL address to the text file (.txt).
Firstly I've managed with copying my URL address to the <textarea> by following this query:
How to copy URL on button click?
It works perfectly for me, although I need a bit more here. I need to have it exported to the text file.
Because I work on a local server, I use the get method in my form. It results in quite a long URL, which can be retrieved (reopened) when copied. I think it could be convenient for me to have it stored somewhere, like in the .txt file.
I used this solution:
https://www.websparrow.org/web/how-to-create-and-save-text-file-in-javascript
for saving dynamic data, although it doesn't work for me.
My code looks like this now:
<script>
function Copy() {
var Url = document.getElementById("url");
Url.innerHTML = window.location.href;
console.log(Url.innerHTML)
Url.select();
document.execCommand("copy");
}
function saveStaticDataToFie() {
var Urltext = document.getElementById("url").value;
var blob = new Blob([Urltext], {type: "text/plain;charset=utf-8"})
saveAs(blob, "survey.txt");
}
</script>
<button type="button" onclick="saveStaticDataToFile();">Click to Save</button>
I see no reaction at all. What have I done wrong here?

Related

How To Set a .json file as a variable for use in window.open (javascript)?

I have made a website using html and javascript. I am Experienced In both, but am struggling with making a button open a website with the url being from a json file. So I want to make a json file a variable, then use it with window.open(JSON-url,"_blank"). I then want to make a var called JSON-url and it must be linked with the .json file.
I had Searched up many examples, but cannot seem to get the correct wordings. I have tried w3schools, Stack Overflow, Quora and many others. I mainly tried other searches for example, "How To Make JSON a variable to use in window.open javascript", but that didn't work either.
{
"JSON-url": "https://www.google.com"
}
I bet you there are a lot who know the answer to this, so please, raise your thoughts!
There's nothing special about JSON files when using a variable for the URL in window.open(). You do it the same as any other URL.
Use the FileReader API to read the file, then use the contents as the URL to open.
function openFile(files) {
if (files.length == 0) {
return;
}
const reader = new FileReader();
reader.onload = function() {
var url = reader.result.trim(); // Contents of the file
console.log("Opening " + url);
window.open(url, "_blank");
}
reader.readAsText(files[0]);
}
Select file:
<input type="file" id="txt" accept="text/plain" onchange="openFile(this.files)">
This won't work in the above snippet because Stack Snippets are sandboxed and don't allow window.open().

document.write and JSON output

I have some big json configs and want allow users to export them. The problem is JS prompt cant return a full data, so i decided to use windows.open and write. But "write" just "eat" html in json values. This answer https://stackoverflow.com/a/22055706 helped a lot.
var data = Store.export();
var url = 'data:text/json;charset=utf8,' + encodeURIComponent(data);
window.open(url, '_blank');
window.focus();
But when i try to export really large and long json everything freezes...
How can i modify it to use blank url and unmodified json as text?
UPD
My workaround
var myWindow = window.open("", "JSON Settings", '_blank');
myWindow.document.write('<textarea>' + escapeHTML(Store.export()) + '</textarea>');
myWindow.focus();
You could include a multi-line text field in which you would place the data.
This gives it a space to go rather than just being injected into the HTML.
The user would then copy and paste the data out into their own .json file.
If you're trying to export the data, do you have any requirements prohibiting the user from downloading the data?
This lib has come in handy for me several times:
https://github.com/eligrey/FileSaver.js

Download zip using jszip in Sharepoint

Hi I am looking for solutions insanely for almost 2 days.I have sharepoint library where multiple word docs uploaded.Using javascript JSZip I like to pass sharepoint file urls into it.Is this possible ?
I am getting each file server relative URL by ecmascript.Now I would like to do something like this:
function create_zip() {
debugger
var zip = new JSZip();
zip.add("http://myspsite/shareddoc/a/test.docx", "\n"); //this is hardcoded for testing??here I have file url
//zip.add("hello2.txt", "Hello Second World\n");
content = zip.generate();
location.href = "data:application/zip;base64," + content;
}
Any pointers or how I proceed from here?
Thanxs.
It´s insanely but interesting. Could works. Creating a button and calling the library and the script, I´m sure of that.
or, try this: http://techtrainingnotes.blogspot.com.ar/2010/05/sharepoint-2010-windows-explorer-view.html
This is more simple.

How to download a file with a very long URL?

Basically I'm trying to trigger the download of a file which is constructed dynamically. In order to construct the file I need to pass a lot of information to the servlet.
This means I can't use GET, I should use POST instead.
In order for the download not to mess the application up, though, I must open a new window. The problem is that I cannot do this:
window.open(http://very.long.url.com/.../, "_blank");
since this would cause the URL limit to be exceeded and GET to be used instead of POST.
All information that needs to be sent to the servlet are in an object called "config.
My solution was to write a form in the new window with POST method, then add hidden inputs to it and then trigger the submit, like this:
var win = window.open("about:blank", "_blank");
win.document.write("<form method='post' id=donwload action='serveletURL'></form>");
$.each(config, function (name, value) {
var fragment = document.createDocumentFragment(),
temp = document.createElement('div');
temp.innerHTML = '<input type=\'hidden\' name=\'' + name + '\' value=\'' + value + '\'></input>';
while (temp.firstChild) {
fragment.appendChild(temp.firstChild);
}
win.document.getElementById("download").appendChild(fragment);
});
win.document.getElementById('download').submit();
And it works in Google Chrome and Firefox, but it doesn't work on IE. On IE (even IE 10), a download dialog with a "serveletURL" file name opens and nothing gets downloaded when I ask the file to be saved or opened.
I'm sure I'm missing something... How do I accomplish this??
Thank you!
Eduardo

creating a file by javascript

i am trying to make a file in javascript, but it doesn't be saved in the root.
when the file is made, the sentence i write has to be stored in the file.
or loading a sentence from a textbox is also fine for me.
can anyone give me help??
function createFile() {
var fso = new ActiveXObject('Scripting.FileSystemObject');
var fileObj = fso.CreateTextFile("G:\\soonrok.txt", true);
fileObj.WriteLine("Hello, I am Soonrok!!!");
}
<td>
<input type="button" value="save" onClick="createFile()">
The best you're going to be able to do with JavaScript is create a cookie.
http://www.w3schools.com/js/js_cookies.asp
Or you could do something that only works for IE with ActiveX.
Or see the link from #AurA for Chrome Only tricks and/or ways to download files from a server.

Categories

Resources