Looking at this DOM structure, we have an image with the following details:
<img id="this-is-the-image" src="http://192.168.1.100/Image_tmp/2016-06/d4eb8d">
The task at hand is to click a button, execute a JavaScript function, and download the image file.
The button implementation and the downloading script are already completed.
Below is a snippet of the code:
function downloadFile(fileName, url) {
var aLink = document.createElement('a');
var blob = new Blob([url]);
var evt = document.createEvent("HTMLEvents");
getImageType(blob);
evt.initEvent("click", false, false);
aLink.download = fileName;
aLink.href = URL.createObjectURL(blob);
aLink.dispatchEvent(evt);
}
I've encountered an issue where I can only extract the source
"http://192.168.1.100/Image_tmp/2016-06/d4eb8d"
or the filename d4eb8d
, while in reality, the image could be in the format of .png
or .jpg
.
Although the browser displays it correctly, when saved, the file name defaults to just d4eb8d
instead of including the extension like d4eb8d.png
or d4eb8d.jpg
.
Is there a method to accurately determine the image type so that the downloaded file includes the correct extension?