Hi I have a case a URL simillar to this:
https://linkhere?response-content-disposition=inline; filename="placeholder.jpg"; filename*=UTF-8''placeholder.jpg response-content-type=image/jpeg&X-Amz-Algorithm=....sometextcontinueshere
I am trying to decode it like this and I need to take the filename
const decodedUrl = decodeURIComponent("linkhere")
const urlParams = new URLSearchParams(decodedUrl);
const filename = urlParams.get('filename');
console.log(decodedUrl)
But for some reason does not work the decoding properly, do you guys have any idea?
There is nothing built in that is magically going to get the filename since it is not a querystring paramter. The easiest thing you can do it change whatever is building this to have valid querystring parameters so you can parse it.
With what you have, you will need to read the one parameter that has the file name in it. After that you are going to have to parse out the filename from that string.
Basic idea:
var str = `https://linkhere?response-content-disposition=inline; filename="placeholder.jpg"; filename*=UTF-8''placeholder.jpg response-content-type=image/jpeg&X-Amz-Algorithm=....sometextcontinueshere`;
const url = new URL(str);
const parts = url.searchParams.get('response-content-disposition').split(/;\s/);
const fileNameParam = parts.find(x => x.startsWith('filename=')).match(/"([^"]+)"/)[1];
console.log(fileNameParam);
Related
I need to append params to my query, but I always get different results. I tried a few ways but without success. Check code below.
First check my code which work without new URL (I need to use new URL).
let url = `${getBaseUrl()}/myUrl/search?surnames=${allData.join('%0A')}`
This works well, but when I use new URL:
let url = new URL('myUrl/search' , getBaseUrl())
url.searchParams.append('surnames' , allData);
The code above doesn't work, and I don't know the reason why?
I tried to inspect url and see different
Works: search?surnames=smith%0Ajordan
Doesn't work: search?surnames=smith%250Ajordan
The only difference is inside between "smith" and "jordan"
With %0A work
With %250A doesn't work
Not sure how this was generated.
The original % is being encoded as %25, so %0 becomes %250.
Try using an unencoded line feed (\n):
const
allData = ['smith', 'jordan'],
getBaseUrl = () => 'https://localhost:8080',
url = new URL('myUrl/search' , getBaseUrl());
url.searchParams.append('surnames', allData.join('\n'));
console.log(url); // https://localhost:8080/myUrl/search?surnames=smith%0Ajordan
I have drag&drop event and I would like to hash the filed dragged. I have this:
var file = ev.dataTransfer.items[i].getAsFile();
var hashf = CryptoJS.SHA512(file).toString();
console.log("hashf", hashf)
But when I drag differents files, "hashf" is always the same string.
https://jsfiddle.net/9rfvnbza/1/
The issue is that you are attempting to hash the File object. Hash algorithms expect a string to hash.
When passing the File Object to the CryptoJS.SHA512() method, the API attempts to convert the object to a string. That conversion results in CryptoJS.SHA512() receiving the same string not matter what File object you send provide it.
The string is [object File] - you can replace file in your code with that string and discover it is the same hash code you've see all along.
To fix this, retrieve the text from the file first and pass that to the hashing algorithm:
file.text().then((text) => {
const hashf = CryptoJS.SHA512(text).toString();
console.log("hashf", hashf);
});
If you prefer async/await, you can put it in an IIFE:
(async() => {
const text = await file.text()
const hashf = CryptoJS.SHA512(text).toString();
console.log("hashf", hashf);
})();
I am encoding the view state in the hash using rison.
Here is an example URL:
http://example.com/board/projects#(date:'2019-01-24',projects:!(5441))
Here is how Gmail recognizes it:
http://example.com/board/projects#(date:'2019-01-24',projects:!(5441))
By the way, SE parser fails to recognize it properly as well:
http://example.com/board/projects#(date:'2019-01-24',projects:!(5441))
Even though all of the characters are valid URL characters, I am getting complaints from users that they can't send the link in gmail (which is actually possible, just doesn't happen automatically).
Is there any other encoding library or method that would encode the json object in the hash that would be safe for parsers such as gmail?
Standard URI encoding should do the job.
const base = "http://example.com/board/projects"
const data = "(date:'2019-01-24',projects:!(5441))"
const encoded_data = encodeURIComponent(data);
const final = base + '#' + encoded_data;
console.log(final);
I am relatively new to JavaScript and I want to get the hash of a file, and would like to better understand the mechanism and code behind the process.
So, what I need: An MD5 or SHA-256 hash of an uploaded file to my website.
My understanding of how this works: A file is uploaded via an HTML input tag of type 'file', after which it is converted to a binary string, which is consequently hashed.
What I have so far: I have managed to get the hash of an input of type 'text', and also, somehow, the hash of an uploaded file, although the hash did not match with websites I looked at online, so I'm guessing it hashed some other details of the file, instead of the binary string.
Question 1: Am I correct in my understanding of how a file is hashed? Meaning, is it the binary string that gets hashed?
Question 2: What should my code look like to upload a file, hash it, and display the output?
Thank you in advance.
Basically yes, that's how it works.
But, to generate such hash, you don't need to do the conversion to string yourself. Instead, let the SubtleCrypto API handle it itself, and just pass an ArrayBuffer of your file.
async function getHash(blob, algo = "SHA-256") {
// convert your Blob to an ArrayBuffer
// could also use a FileRedaer for this for browsers that don't support Response API
const buf = await new Response(blob).arrayBuffer();
const hash = await crypto.subtle.digest(algo, buf);
let result = '';
const view = new DataView(hash);
for (let i = 0; i < hash.byteLength; i += 4) {
result += view.getUint32(i).toString(16).padStart(2, '0');
}
return result;
}
inp.onchange = e => {
getHash(inp.files[0]).then(console.log);
};
<input id="inp" type="file">
here i have variable fileVal takes path of file like uploads/import/abc.pdf
var fileVal = $('#fileimport').val();
here fileVal takes path of file like uploads/import/abc.pdf
i have to send this file path as url variable to a php file
and then display result on messagediv.
like
$('#messagediv').load('../xyz.php?file='+fileVal);
here without url variable, its working perfect getting values to the messagediv.
But where as using url variable its not.
How i can solve this?
try with encodeURIComponent()
var fileVal = encodeURIComponent($('#fileimport').val());
That should work fine except you have a syntax error
// $var fileVal = $('#fileimport').val();
var fileVal = $('#fileimport').val();