I wanted to insert the prefix before the file extention.
#Sample filename
DOC.doc
#code
let filename = "Doc.doc"
const prefix = Date.now().toString();
filename += prefix;
#Desired output (1595214202266 is the prefix.)
DOC-1595214202266.doc
Just split the fileName on "." and concat the prefix to file and make the string.
const prefix = Date.now().toString();
var fileName = "DOC.doc"
var [name, ext] = fileName.split('.');
console.log(`${name}-${prefix}.${ext}`)
let filename = "Doc.doc"
const prefix = Date.now().toString();
let extensionIndex = filename.indexOf(".");
filename = filename.substr(0,extensionIndex) + "-" +
prefix + filename.substr(extensionIndex,filename.length);
console.log(filename);
Related
I am trying to generate a vCard .vcf file and download it on click. It works perfectly for all browsers except Safari. On safari, it downloads example.com instead of the proper file. I read a lot about the Safari download attribute issue, but it seems this is not the problem in my case. Any help would be highly appreciated.
This is my crappy code :)
let vCard = document.querySelector('.vcard')
let vcard_start ="BEGIN:VCARD\nVERSION:3.0\n"
let vcard_end = "END:VCARD"
let fullName = document.querySelector('#fullname').innerText
//let firstName = document.querySelector('#firstname').innerText
//let lastName = document.querySelector('#lastname').innerText
let title = document.querySelector('#title').innerText
let email = document.querySelector('#email').innerText
let phone = document.querySelector('#phone').innerText
let address = document.querySelector('#address').innerText
let vcard_download = document.querySelector('#vcard-download')
let currentURL = window.location.href
let attPhoto = document.querySelector('#att_image')
let attPhotoSRC = attPhoto.querySelector('img').src
let splitName = fullName.split(' ')
//let vCardPhoto =
//const space = split(/\r?\n/);
let vcardReady
let newName
if(splitName.length>=3){
newName = splitName[2] + ';' + splitName[0] + ' ' + splitName[1]
}else{
newName = splitName[1] + ';' + splitName[0]
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href','data:text/vcard;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
let base64_image_encoded;
window.addEventListener('load',function(){
// Start file download.
vcard_download.addEventListener("click", function(){
const tmp = base64_image_encoded.split(",");
// Generate download of vCard file with some content
vcardReady = vcard_start+
"FN:"+fullName+ "\n" +
"N:" + newName + "\n" +
"EMAIL:" + email + "\n" +
"ORG:Carella, Byrne, Cecchi, Brody & Agnello, P.C\n" +
"ROLE:" + title + "\n" +
"TEL;TYPE=WORK;VOICE:" + phone + "\n" +
"URL:" + currentURL + "\n" +
"PHOTO;TYPE=JPEG;ENCODING=BASE64:"+ tmp[1] + "\n" +
"NOTE;CHARSET=us-ascii;ENCODING=QUOTED-PRINTABLE:" + "\n" +
vcard_end;
var text = vcardReady;
var filename = fullName+'.vcf';
download(filename, text);
});
toDataURL(attPhotoSRC, function (dataUrl) {
base64_image_encoded = dataUrl;
});
});
function toDataURL(src, callback, outputFormat) {
let image = new Image();
image.crossOrigin = 'Anonymous';
image.onload = function () {
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
let dataURL;
canvas.height = this.naturalHeight;
canvas.width = this.naturalWidth;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
};
image.src = src;
if (image.complete || image.complete === undefined) {
image.src = "data:image/gif;base64, R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
image.src = src;
}
return image;
}
I have seen this issue when there was a newline character in the filename for Safari. Removing that newline did the trick for me
let NewName= params.strdocumentname + "(" + results.rows[0].arrayfilecount+ ")";
strdocumentname = params.strdocumentname = NewName;
the current output is Jayson.png(2)
desired output Jayson (2).png
How do i do that using the code above?
Use the following code:-
const path = require('path');
const extension= path.extname(params.strdocumentname);
const name= params.strdocumentname.replace(extension,"(" + results.rows[0].arrayfilecount+ ")");
const newName = name + extension;
Hope it helps
Thanks
I think your params.strdocumentname contains the extension of the file also.
Try this code;
let [fileName, extension] = params.strdocumentname.split('.');
let NewName= fileName + "(" + results.rows[0].arrayfilecount+ ")." + extension;
strdocumentname = params.strdocumentname = NewName;
see working fiddle
let mockFileName = "abc.png"
let mockArrayCount = '1'
let [fileName, extension] = mockFileName.split('.');
let NewName= fileName + "(" + mockArrayCount+ ")." + extension;
console.log(NewName)
alert(NewName)
This is what I have so far but it returns only one proxy because it rewrites over it x (however many proxies) times. I do not want to make a new file but instead rewrite proxies.txt with every proxy.
const fs = require("fs");
const formatProxies = () => {
const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
const split = rawProxies.trim().split("\n");
for (const p of split) {
const parts = p.trim().split(":");
const [ip, port, user, pass] = parts;
fs.writeFileSync(
"./proxies.txt",
user + ":" + pass + "#" + ip + ":" + port + "\r\n",
{ encoding: "utf8" }
);
}
};
formatProxies();
Does this work?
const fs = require("fs");
const formatProxies = () => {
const rawProxies = fs.readFileSync("./proxies.txt", "utf-8");
const split = rawProxies.trim().split("\n");
const lines = []
for (const p of split) {
const parts = p.trim().split(":");
const [ip, port, user, pass] = parts;
lines.push(user + ":" + pass + "#" + ip + ":" + port)
}
fs.writeFileSync(
"./proxies.txt",
lines.join("\r\n"),
{ encoding: "utf8" }
);
};
formatProxies();
Node.js has fs.appendFileSync, which writes to the end of the file instead of overwriting the whole thing.
I have a url and i need to enter a port number to the url.
the url is not a valid url.
here is few show cases :
https://example.com_users/param/param/param - https://example.com_users:8080/param/param/param
http://example.co_setting/param/param/param - http://example.co_settings:1000/param/param/param
http://example.co_setting- http://example.co_settings:1000
const addPort = (url,port) =>{
combined = ???????? // how to combian them
return combined
}
You could use a regular expression:
const addPort = (url, port) =>
url.replace(/^(https?:\/\/)?([^/]*)(\/.*)?$/, '$1' + '$2:' + port + '$3');
console.log(addPort('http://www.example.com/full/url/with/param', '8080'))
var urlstring = 'https://example.com_users/param/param/param';
var port = ':8080';
var allparts = urlstring.split('//');
var last = allparts[1];
var alllastparts = last.split('/');;
alllastparts[0] = alllastparts[0]+port;
alert(allparts[0]+ '//' + alllastparts.join('/'));
console.log(allparts[0]+ '//' + alllastparts.join('/'));
I currently have a block of code that exports tables in my HTML body to one excel file.
$(document).ready(function() {
$("#btnExport").click(function(e) {
//getting values of current time for generating the file name
var dt = new Date();
var day = dt.getDate();
var month = dt.getMonth() + 1;
var year = dt.getFullYear();
var hour = dt.getHours();
var mins = dt.getMinutes();
var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
//creating a temporary HTML link element (they support setting file names)
var a = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementsByClassName("dvData");
var table_html = table_div[0].outerHTML.replace(/ /g, '%20');
var table_html2 = table_div[1].outerHTML.replace(/ /g, '%20');
var table_html3 = table_div[2].outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html + "<br></br>" + table_html2 + "<br></br>" + table_html3;
//setting the file name
a.download = 'exported_table_' + postfix + '.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
e.preventDefault();
});
});
</script>
When the excel file opens, I want it to have some vba code that automatically runs when the user opens the downloaded file.
Is this possible? If so, how do I "embed" the vba code into the excel file my code exports?