Auto User Location Detection

For security, analytics, or auditing purposes, logging IP addresses and their derived locations alongside uploaded files is recommended. Filestack and IPstack together provide a solution: 

  • Filestack offers reliable file upload, transformation, and storage.
  • IPstack: Instant geolocation and threat intel on any IP. It tells you where the request came from.

Try it yourself:

In this demo, we use IPstack API to retrieve user location information. Block uploads from certain locations and log location with uploaded file information.

 

Terminal

npm install filestack-js tslib

HTML

JavaScript (index.mjs)

import "./styles.css";
import * as filestack from "filestack-js";

const filestackApiKey = "YOUR_API_KEY";
const ipstackAccessKey = "IPSTACK_ACCESS_KEY";

const blockedCountries = ["US", "EG"]; // Example

// Cache location data to avoid multiple API calls
let locationData = null;

document
  .getElementById("openPicker")
  .addEventListener("click", async function () {
    try {
      // Get location data (using cache if available)
      if (!locationData) {
        locationData = await getLocation();
      }

      // Check if country is blocked
      if (blockedCountries.includes(locationData.country_code)) {
        console.log("Upload is blocked from your location");
        return false;
      }

      // Open FileStack picker
      openFilePicker();
    } catch (error) {
      console.error("Error in file picker click handler:", error);
      showError("Unable to process your request. Please try again.");
    }
  });

/**
 * Opens FileStack picker with predefined options
 */
function openFilePicker() {
  const client = filestack.init(filestackApiKey);
  const pickerOptions = {
    maxFiles: 1,
    onUploadDone: displayImageWithLocation,
  };
  const picker = client.picker(pickerOptions).open();
}

/**
 * Displays image preview with location information
 * @param {Object} files - Object containing uploaded file data
 */
function displayImageWithLocation(files) {
  if (files.filesUploaded.length === 1) {
    const { url, filename } = files.filesUploaded[0];
    const { city, region_name, country_name } = locationData;

    // Update the HTML content to show image preview and location info
    document.getElementById("app").innerHTML = `
      

Preview Link: ${url}

File Name: ${filename}

Uplaoded From: ${city}, ${region_name}, ${country_name}

`; } else { // Handle case where no file or multiple files were uploaded document.getElementById("app").innerHTML = `

File upload failed. Try again!

`; } } /** * Retrieves location data from the ipstack API * @returns {Promise} - Promise that resolves with location data or rejects with error */ async function getLocation() { // Construct the API URL with access key const url = `https://api.ipstack.com/check?access_key=${ipstackAccessKey}&output=json`; const options = { method: "GET", headers: { Accept: "application/json" } }; try { const response = await fetch(url, options); const data = await response.json(); console.log(data); return data; } catch (error) { console.error(error); } }

Start building your own file upload solution today.

PREVIEW

Users can preview their uploaded files directly inside the File Picker.

CONNECT TO 20+ CLOUD DRIVES

Connect your app directly to AWS, Facebook, Dropbox, Instagram, and more.

PROGRESS BAR UPLOADS

Give your users confidence that their files are on the way to the right destination.

MULTI-FILE UPLOADS

Let your users upload multiple files at one time for speed and simplicity.

CUSTOM CSS

Filestack blends seamlessly into your website or app. Ask about white-labeling.

CLIENT-SIDE CROP

Users can crop files to perfection before sending them to your website or app.

UNLIMITED UPLOADING

Upload any file type, from any local or cloud source, at any scale.

WEBSITES & HTML

Filestack can even load web content from any public URL and capture the rendered page as an image. Perfect for generating screenshots, previews, or converting HTML-based content into other formats.

SECURE CLOUD STORAGE

Store uploads directly in your cloud storage for ease and accessibility.

“Being smart here, in my opinion, is seriously looking at Filestack to give you a fantastic uploading experience, while you spend your time on your product vision, not already-solved problems.”

— CHRIS COYIER, FOUNDER OF CSS-TRICKS & CODEPEN