How to scan a file for viruses in Javascript

Proper cyber security is essential in this digital day and age. Any time you take in files from the public, viruses and malware are going to rear their ugly heads. This tutorial is going to get you set up with advanced virus scanning in no time using Javascript.

Let’s dive in with setting up our API client. This can be done by inserting this script tag into your HTML file:

<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-virus-client.js"></script>

Once that has finished importing, we can proceed with implementing our function.

var CloudmersiveVirusApiClient = require('cloudmersive-virus-api-client');var defaultClient = CloudmersiveVirusApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveVirusApiClient.ScanApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.scanFile(inputFile, callback);

Now submit your file and wait a moment for your results, which can take a few moments if the file is very large or heavily compressed. This function applies to virtually any file type, including archives.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store