Writing in file and saving it from Save as dialog through javascript - javascript

Below code is working fine to write XML data into a file.
function btnSave_onclick() {
var aFSO = new ActiveXObject("Scripting.FileSystemObject");
aTS = aFSO.OpenTextFile(FilePath,2,true);
aTS.Write (XMLText.value);
aTS.Close();
}
I want to change this code to show a dialog box to saveAs in desired location.
I found below code to open save as dialog box
document.execCommand('SaveAs','true',filename.txt);
Now question is how can I save data (which is in XMLText.value) through This dialog.
Is there any other way to save data trogh dialogBox. It is for IE
In other way Is it possible to provide a Browse button to select desired location in c: drive or any other drive and by saying ok get that path and store in a variable and by using above code can save directly to that location.
But here question is how to provide browse button?

Related

Storing file chooser data in a hidden input

I am using a tool called TamperMonkey to modify a webpage for my personal convenience. TamperMonkey lets you modify the client-side HTML etc. by appending JavaScript to the page after it loads.
The webpage has a file chooser, but it doesn't let you drag-and-drop a file. I use this webpage a lot so having drag-and-drop functionality would really help.
This page has a form with a file input type. I've been reading, and there's no way to modify a file input type for security reasons. But, using TamperMonkey, I could change the input type of the file chooser to "hidden," then set the hidden input value to the file contents that I drop on the webpage, right? It's my understanding that the server couldn't tell the difference (if the name attribute is the same).
I don't know how to set the hidden input type to the same data the file chooser would have:
I've got my file here: const file = e.originalEvent.dataTransfer.files[0];
I've got my hidden input type by doing this: const hiddenField = $("iframe").contents().find(".file-input").attr('type','hidden')
I just don't know how to take file and set it on hiddenField. Should this value be base64 encoded? A blob? Regardless, what code would set the data correctly?
The form it's wrapped in looks like this:
<form ... method="post" enctype="multipart/form-data">
Important context for alternative suggestions: this file chooser input isn't on the page at all until you click a button. Then it shows up in an iframe.
I am using firefox.
It is possible to modify a file input type with Tampermonkey.
If you are not able to add drag and drop support to the input itself straight away, you can implement drag and drop using another dom element and use them as below.
const dT = new ClipboardEvent('').clipboardData || // Firefox < 62 workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655
new DataTransfer(); // specs compliant (as of March 2018 only Chrome)
dT.items.add(new File([dataURItoBlob(image)], '1.jpg', { type: 'image/jpeg' }));
dT.items.add(new File([dataURItoBlob(image2)], '2.jpg', { type: 'image/jpeg' }));
// then ...
// $("iframe").contents().find(".file-input").get(0).files = dT.files;
That snippet of code was tested working, in a similar setting where a file input was being attached to the page dynamically after clicking a button. The images were drag n dropped onto the page using a different div.

How to Open File Dialog box on SPA (browser side) with custom file list (of remote, server directory)

I have a MVC project with a view. Have a directory on server with files, and have to show good looking Open File dialog box with this directory and files in it (not a user's local directory). I got a list of files, and path, and send as variables (string and array of strings) into my view, so now I can show directory listing on my page. But I'd like to use some standard looking dialog box for choosing file instead of writing my own window with radio buttons and text boxes.
Is there any way to fill Open file dialog box with my list of files With Javascript?
Regarding the buttons, you can take a look at bootstrap: https://getbootstrap.com/docs/4.0/components/list-group/
There you can use predefined buttons, lists etc. But specifically for downloading files there is no SPECIAL button of any kind.
Regarding the JS part, you can use something like:
<a id="download_link" download=filename.txt" href=”” onclick="getFile('someinputtxt')"><font size="5">Download Txt File</font></a>
<script>
function getFile(input)
{
var text = input;
var data = new Blob([text], {type: 'text/plain'});
var url = window.URL.createObjectURL(data);
document.getElementById('download_link').href = url;
}
</script>
There I create a text file with some input. But basically you can use a button/list that is already in bootstrap for example and just add the file using 'download':
Hope it helps.

Adobe Acrobat (X): Convert between DataObject and Icon objects

My goal is to have the user press a button to select a file ONE TIME that will be used both as an attachement and the button icon. I know how to do each of these tasks separately, but this requires my user to select the same file twice. I am looking for a way to either:
Have them import a file as a button Icon, then change the Icon into a DataObject and attach it to the pdf
Have them import a file as a DataObject attachment, then convert it to an Icon
Is this possible? I haven't been able to figure this conversion step out.
Thanks
You can prompt the user for the file to import once and then use the path for the two use cases you need.
var img = app.browseForDoc(); // prompts the user to select the file
var imgPath = img.cPath; // get the device independent path to the file
this.importDataObject("myDataObjectFileName", imgPath); // import the data object
this.getField("myImageButton").buttonImportIcon(imgPath); // set "normal" the button face
Your button must be set up to show an icon first and you'll also want to set the scaling properties as well. You can make the attachment name the same as the file name with some simple string functions on the path.

how can I send input straight to textarea

I am using
https://github.com/Rovak/InlineAttachment
I currently have it all setup so I can drag and drop images to my textarea and they upload straight to imgur, from following this tutorial:
http://wilfreddenton.com/posts/drag-and-drop-photo-uploads-to-imgur
what I'm trying to do is add an input so if the user doesnt want to drag and drop, they can simply browse their pc and upload files, and have them go straight into the textarea upon selection. (just how github comments work)
I just cant seem to get the input to insert into the textarea.
Here's a live example: http://codepen.io/DrCustUmz/pen/KzZOeP
or if your logged into github you can see the end goal what im trying to do: https://github.com/wilfreddenton/dynamic-scss/issues/new
dont actually submit but click "selecting them" below the textarea, pic a few images, and watch the textarea after you click open.
the input selection is not working in this example. How can I get it so I open the choose files, select a few images, then when I select "open" on the browse my pc popup, they are directly inserted into the textarea, just like i drag and dropped them.
Most of the writting work is done in background so I had to rewrite the writting to textarea. Sorry I mostly use pure javascript so I didn't use any fancy jquery features.
This is where the magic happens. The upload can handle only one file at a time so I had to use loop for all of them. I recommend you take a look at the whole code.
document.getElementById("inputFile").addEventListener("change", function() {
var object = this;
var editor = document.getElementById("editor");
for(var i=0;i<this.files.length;i++) {
editor.value += "uploading...";
uploadHandler.customUploadHandler(this.files[i], function(result) {
editor.value = editor.value.replace("uploading...",result.filename);
});
}
});
Whole code on CodePen
You can simply attach the eventListener upon file is selected via choose files option. You just have to trigger the upload in callback. Assume that you've given id of the input type file as "fileUpload"
document.getElementById('fileUpload').addEventListener('change', function () {
console.log(this.files);
/*trigger the upload here, like you're doing in drag and drop*/
});

Use Webpage to Open it in Excel

I'm looking fora functionality that if you press a button or a link inside of a website, it will automatically open excel in you local computer. The button or link that you press is defined with specific data for instance data "test" and you want to have the data inside of the excel after you have pressed the button or link
Please remember that you have the data in the webpage and then when you press on the button or link you open the excel document and the data from the wepage will be transer automatically to the excel document.
Is it possible to do it? if yes, how?
I have tried finding a solution by using sourcecode but I failed.
You can use the ms-excel scheme to open a file in Excel.
A simple example:
<a href="ms-excel:ofe|u|http://berkeleycollege.edu/browser_check/samples/excel.xls">
Open in Excel
</a>
Demo: http://jsfiddle.net/DerekL/mp0p1gcq/
Another demo: http://jsfiddle.net/DerekL/x9npn97z/
Full Syntax Definition for the ms-excel scheme:
Excel Scheme = "ms-excel:" open-for-edit-cmd | open-for-view-cmd | new-from-template-cmd
open-for-edit-cmd = "ofe|u|" document-uri
open-for-view-cmd = "ofv|u|" document-uri
new-from-template-cmd = "nft|u|" template-uri ["|s|" save-location]
document-uri = URI location of document to open
template-uri = URI location of template file upon which new file will be based
save-location* = URI location of folder in which new document should be created
*save-location is an optional parameter

Categories

Resources