Scan a File for Viruses and Threats in JavaScript

Cloudmersive
2 min readMar 23, 2021

You won’t get far these days without having at least one, and in some cases several, security precautions set up for your business. To prevent your employees or users from being the victim of a cyberattack, we have developed an API that can deliver complete content protection from behind the scenes. Parameters for the process can be set to allow or block several variables including executable files, invalid files, scripts, password-protected files, and macros; this will ensure that your content is safe from all angles.

To start using the API in JavaScript, you first need to install the jQuery library:

bower install jquery

After the installation, we need to call the virus scan function, which we can do with the following code:

var form = new FormData();
form.append("inputFile", fileInput.files[0], "file");
var settings = {
"url": "https://api.cloudmersive.com/virus/scan/file/advanced",
"method": "POST",
"timeout": 0,
"headers": {
"allowExecutables": "<boolean>",
"allowInvalidFiles": "<boolean>",
"allowScripts": "<boolean>",
"allowPasswordProtectedFiles": "<boolean>",
"allowMacros": "<boolean>",
"restrictFileTypes": "<string>",
"Content-Type": "multipart/form-data",
"Apikey": "YOUR-API-KEY-HERE"
},
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};
$.ajax(settings).done(function (response) {
console.log(response);
});

Your result will be delivered immediately, allowing you to determine if the file should be accepted or rejected. To retrieve your personal API key, visit the Cloudmersive website to register for a free account; this will give you access to 800 monthly calls!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.