Fake file upload in Chrome not showing file name - javascript

I'm trying to use the solution over at quirksmode.org to sort the file upload input inconsistency problem, using the following javascript:
$('.sidebar-uploadcv input[type=file]').attr('onchange','javascript:document.getElementById("fakeupload").value = this.value').addClass('file_input_hidden');
The trouble is, this doesn't work properly in Chrome. It renders and the user can click to choose a file, but the filename isn't then displayed in the #fakeupload input.
Can anyone help?

Found the solution by invoking .change() instead of .attr('onchange',...). This method works cross-browser.
To get rid of the fakepath string in Webkit et al I've also added the var filename... line to strip out that part of the file name.
$('.input[type=file]').change( function() {
var filename = $(this).val().replace(/C:\\fakepath\\/i, '');
$('#fakeupload').val( filename );
});

Related

Problem passing file to an input-type-file webelement

How to set File objects and length property at FileList object where the files are also reflected at FormData object?
I was trying to use the hack described in the link above to insert through code the file I want to upload to an '<input type='file', but I am having trouble to implement it.
I am using the following code as a snippet in Chrome devtools Console Panel:
inp = document.querySelector("input[type=file]");
const dT = new DataTransfer();
dT.items.add(new File(['foo'], 'C:\\Users\\MyUser\\Desktop\\SomeFolder\\MyTestFile.txt'));
inp.files = dT.files;
The input text that should display only the file name shows the full path, and when I press the button to upload the file it return an error saying that the file was not founded.
I already check the path and it is 100% right. I don't know much about javascript, in fact I am using Selenium to do the rest of the automation, but this particular subject looks like can only be achieved with js.
Could anyone help me? Am I mising something or did I misunderstood the hack solution and what I am trying to do is not possible at all?

set file attribute filesystemobject javascript

I have created a file as part of a script on a network drive and i am trying to make it hidden so that if the script is run again it should be able to see the file and act on the information contained within it but i am having trouble doing this. what i have so far is:
function doesRegisterExist(oFs, Date, newFolder) {
dbEcho("doesRegisterExist() triggered");
sExpectedRegisterFile = newFolder+"\\Register.txt"
if(oFs.FileExists(sExpectedRegisterFile)==false){
newFile = oFs.OpenTextFile(sExpectedRegisterFile,8,true)
newFile.close()
newReg = oFs.GetFile(sExpectedRegisterFile)
dbEcho(newReg.Attributes)
newReg.Attributes = newReg.Attributes+2
}
}
Windows Script Host does not actually produce an error here and the script runs throgh to competion. the only guides i have found online i have been attempting to translate from VBscript with limited success.
variables passed to this function are roughly declared as such
var oFs = new ActiveXObject("Scripting.FileSystemObject")
var Date = "29-12-2017"
var newFolder = "\\\\File-Server\\path\\to\\folder"
I know ActiveX is a dirty word to a lot of people and i should be shot for even thinking about using it but it really is a perfect fit for what i am trying to do.
Please help.
sExpectedRegisterFolder resolves to \\\\File-Server\\path\\to\\folder\\Register which is a folder and not a file.
I get an Error: file not found when I wrap the code into a try/catch block.
I tested the code on a text file as well, and there it works.
So you're either using the wrong method if you want to set the folder to hidden.
Or you forgot to include the path to the text if you want to change a file to hidden.
( Edit: Or if Register is the name of the file, add the filetype .txt ? )
If you change GetFile to GetFolder as described in https://msdn.microsoft.com/en-us/library/6tkce7xa(v=vs.84).aspx
the folder will get hidden correctly.

JavaScript to .csv download fails: "Failed. No File"

Right now I'm just trying to get anything to work. My browser is Chrome 40.022. When I click my download link, it initiates downloading but then fails with the error Failed. No file. What am I doing wrong? Is this a browser thing?
Javascript:
var fileName = "data.csv";
var csv = "abc, def, hij";
//I'm partially using angular in this page...
$scope.link3 = 'data:text/csv;base64,' + btoa( csv ); //forgive the variable name
HTML:
<a ng-href="aap.link3" download="data.csv">Hello</a>
I created a jsfiddle for your problem. There are 2 issues with your code:
In your case, ng-href data need to be interpolated
You must add data into whitelist for Href attrtibute, else it will
be transformed into 'unsafe'.
https://jsfiddle.net/8o1v1uym/

Get the name of a Thunderbird attachment

Is there a way to get the filename and/or filepath of an attachments from the current TB message?
I tried this (HERE is the link):
function AttachmentNames() {
var attachments = instanceOfMozMmsMessage.attachments;
}
with different alerts like attachments.length,attachments[0].filepath etc., but nothing happened (i guess it's improper for current mails anyway).
See http://www.xulforum.org/mozcamp2012/dzslides/template.html#34.2 along with the demo addon, or http://jonathan.xulforum.org/files/glodebug.xpi
There's also https://developer.mozilla.org/en-US/Add-ons/Thunderbird/Demo_Addon

Create file to download js

I'm receiving a JSON output from an API, and i want to use it with JS and also save that output to file. Is there any way to trigger the browser download machanism using JS with thet output?
Maybe now some browsers support the attribute download but you don't trigger the browser to automatically download the file.
the only way i know is #BAS's solution but without a filename.
tested in chrome ..
array=[{a:'1',b:'2'},{x:'3',y:'4'}];
function dl(array,filename){
var b=document.createElement('a');
b.download=filename;
b.textContent=filename;
b.href='data:application/json;base64,'+
window.btoa(unescape(encodeURIComponent(JSON.stringify(array))))
// or
// b.href='data:application/javascript;charset=utf-8,'+JSON.stringify(json);
return b
}
document.body.appendChild(dl(array,'my.json'));
example
http://jsfiddle.net/8yQcW/
UPDATE direct download works ... on chrome i tested.
on append simulate click
var e=document.createEvent('Events');
e.initEvent('click',true,false);
document.getElementById('dl').dispatchEvent(e);
http://jsfiddle.net/8yQcW/1/
You could try it with a data URI:
function triggerDownload(json) {
var dataUri = 'data:text;charset=utf-8,' + json;
window.open(dataUri);
}
Try entering data:text;charset=utf-8,{"Key":"Value"} into your browsers address bar. It should show you the save dialog.
See here for more information and browser support.

Categories

Resources