Open link on file excel on Browser by code JavaScripts - javascript

I have file excel 100 link, I want to open link on Browser.
My code:
const ExcelJS = require('exceljs');
const open = require('open');
const workbook = new ExcelJS.Workbook();
const filePath = 'D:\\3.py\\link1.xlsx';
workbook.xlsx.readFile(filePath)
.then(() => {
const worksheet = workbook.getWorksheet('Sheet1');
const links = worksheet.getColumn('B').values;
links.forEach(link => {
open(link, { target: '_blank' });
});
});
But error:
TypeError: Expected a target
at open (D:\3.py\node_modules\open\index.js:229:9)
Can you help me? Thanks!

Just replace code
open(link.hyperlink, { target: '_blank' });
with it

Related

Downloading an mp3 file from S3 and manipulating it results in bad file

I did a script that downloads a MP3 file from my S3 bucket and then manipulates in before download (Adding ID3 Tags).
It's working and the tags are injected properly, but the files corrupts as it seems and unplayable.
I still can see my tags trough MP3tag so it has data in it, but no audio is playing trough the file.
Heres my code,
Trying to figure it what went wrong
const downloadFileWithID3 = async (filename, downloadName, injectedEmail) => {
try {
const data = await s3Client.send(
new GetObjectCommand({
Bucket: "BUCKETNAME",
Key: filename,
})
);
const fileStream = streamSaver.createWriteStream(downloadName);
const writer = fileStream.getWriter();
const reader = data.Body.getReader();
const pump = () =>
reader.read().then(({ value, done }) => {
if (done) writer.close();
else {
const arrayBuffer = value;
const writerID3 = new browserId3Writer(arrayBuffer);
const titleAndArtist = downloadName.split("-");
const [artist, title] = titleAndArtist;
writerID3.setFrame("TIT2", title.slice(0, -4));
writerID3.setFrame("TPE1", [artist]);
writerID3.setFrame("TCOM", [injectedEmail]);
writerID3.addTag();
let taggedFile = new Uint8Array(writerID3.arrayBuffer);
writer.write(taggedFile).then(pump);
}
});
await pump()
.then(() => console.log("Closed the stream, Done writing"))
.catch((err) => console.log(err));
} catch (err) {
console.log(err);
}
};
Hope you can help me solve this wierd bug,
Thanks in advance!
Ok so i've figured it out, instead of using chunks of the stream itself i've used getSignedUrl from the s3 bucket it works.
Thanks everyone for trying to help out!

javascript cannot convert undefined or null to object question

I am trying javascript for the first time and I am having this trouble with the example:
https://www.twilio.com/blog/web-scraping-and-parsing-html-with-node-js-and-cheerio
It is a web scrapper example that uses got and cheerio, both of which I have installed. But when i run the sample code it gives me 'cannot convert undefined or null to object error.
Why is that? I didn't change anything from the example at all.
the code in question:
const $ = cheerio.load(response.body);
$('a').each((i, link) => {
const href = link.attribs.href;
console.log(href);
});
}).catch(err => {
console.log(err);
});
How does your index.js file look like? I did the tutorial and my code is working. Maybe you are miss typed the url?
Here is my index.js
const fs = require("fs");
const cheerio = require("cheerio");
const got = require("got");
const vgmUrl = "https://www.vgmusic.com/music/console/nintendo/nes";
got(vgmUrl)
.then((response) => {
const $ = cheerio.load(response.body);
$("a").each((i, link) => {
const href = link.attribs.href;
console.log(href);
});
})
.catch((err) => {
console.log(err);
});

How to get <input tag> from file path Folder Name in ReactJs

I using FileReader and I am trying but I got only getting FileName and FileFormat.
However, I couldn't How to get folderName Soma07
There are several modules available but I don't know how to make it work in React.
Does anybody know?
Here are the examples I found:
const [fileName, setfileName] = useState("")
const upLoadImage = async (e) => {
const file = e.target.files[0]
const base64 = await convertBase64(file);
console.log(base64);
setfileName(file.name)
}
const convertBase64 = (file) => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = (() => {
resolve(fileReader.result)
})
fileReader.onerror = ((error) => {
reject(error)
})
})
}
Due to security reasons browsers don't allow to get file path. JavaScript in browser has no access to the File System.

How to upload image to Google Cloud with multer node js

I have done upload using Multer in NodeJS if storage is memoryStorage, since file is save in buffer first, and than from buffer I can upload to Google Drive,
But if using memoryStorage I can not rename image file,
I using multer.diskStorage but when I post, file is succeed upload but not the picture, file size become small like 10B.
this is my code in helper with function uploadImage
const util = require('util')
const gc = require('../config/')
const bucket = gc.bucket('jsimage')//bucket name
const { format } = util
const uploadImage = (file) => new Promise((resolve, reject) => {
console.log(file);
//const { originalname, buffer } = file
const { filename, destination } = file
//const blob = bucket.file(originalname.replace(/ /g, "_"))
const blob = bucket.file(filename)
const blobStream = blob.createWriteStream({
resumable: false
})
blobStream.on('finish', () => {
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
)
resolve(publicUrl)
})
.on('error', () => {
reject(`Unable to upload image, something went wrong`)
})
//.end(buffer)
.end(destination)
})
module.exports = uploadImage
with code above I succeed to upload in Google Drive but not the picture, since size is always 10B.
in this example, after the picture is uploaded to temp or any local folder, we can upload it to google cloud.
const util = require('util')
const gc = require('../config/')
const bucket = gc.bucket('jsimage')//bucket name di google drive
const path = require('path')
const { format } = util
// promises are built right away, so there's no need for then to resolve and catch for errors
const uploadImage = (file) => new Promise((resolve, reject) => {
//console.log(file);
const {filename} = file;
const picture = path.join(__dirname,'../uploads/',filename);
// This is the upload command
bucket.upload(picture);
// This is sent to return
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${filename}`
)
resolve(publicUrl)
reject(err=>(err))
})
module.exports = uploadImage

How can I set the downloaded filename when using Filereader API?

I have a document as a base64string and then I generate the File from this and provide a download link. This all works correctly, however it gives the file a random number rather than the name I want.
Firstly I create a new file like so:
export const urlToFile = (url, filename, type) =>
fetch(url)
.then(res => res.arrayBuffer())
.then(
buffer =>
new File([buffer], filename, {
type
})
);
export const getExportFileUrl = resp =>
urlToFile(
`data:${docType};base64,${resp}`,
`Document_Filename`,
`${docType}`
).then(file => file);
I then combine Filereader and URL Api's to create the download link like so:
// This is returned from the File API.
const exportedFile = {
name: "Document_Filename"
lastModified: 1587577801489
lastModifiedDate: '....'
webkitRelativePath: ""
size: 8243
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}
handleDownloadClick = () => {
const fileReader = new FileReader();
fileReader.onload = () => {
const downloadUrl = URL.createObjectURL(exportedFile);
window.location = downloadUrl;
};
fileReader.readAsText(exportedFile);
};
The filename downloaded is something like:
f8477d6a-bea9-4a83-843f-26e381249b71.docx
How can I set my own filename, is this possible with the above implementation?
The solution was to utilize an anchor element rather than a button and then set the download attribute with the name I wanted for the file like so:
<a download="Name_2020_04_23_12_39" href="blob:http://domain">Download</a>

Categories

Resources