An open-source cross-platform alternative to AirDrop
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LocalSend</title>
<style>
body {
font-family: sans-serif, system-ui;
margin: 0;
line-height: 1.5;
text-align: center;
}
#content {
box-sizing: border-box;
width: 100%;
max-width: 400px;
margin: 15vh auto 0;
padding: 1em;
border: 2px dashed transparent;
border-radius: 0.5em;
}
#content.drag-over {
border-color: #00796B;
background-color: #E0F2F1;
}
#upload-button {
width: 100%;
padding: 1em;
font-family: inherit;
font-size: 1.5em;
color: #FFFFFF;
background-color: #00796B;
border: none;
border-radius: 0.5em;
cursor: pointer;
}
#upload-button:hover {
background-color: #009688;
}
#status-text {
display: none;
font-size: 1.5em;
}
#status-text.error {
color: #C62828;
}
#progress-text {
display: none;
font-size: 1.5em;
}
#drop-hint {
margin-top: 0.75em;
color: #616161;
}
</style>
</head>
<body>
<noscript>
LocalSend requires JavaScript, which is currently disabled. Please enable it and try again.
</noscript>
<h1 style="padding: 0.5em">LocalSend</h1>
<div id="content">
<button id="upload-button" type="button">Upload</button>
<p id="drop-hint">Place items to share.</p>
<input id="file-input" type="file" multiple style="display: none">
<p id="status-text"></p>
<p id="progress-text"></p>
</div>
<script>
var BASE_URL = '/api/localsend/v2';
var i18n = {};
var queryParams = location.search.slice(1).split('&');
var pin = null;
var uploading = false;
// dragenter/dragleave fire for every nested element, so the drop zone is only left when this reaches zero.
var dragDepth = 0;
// Parse query parameters manually for IE
for (var i = 0; i < queryParams.length; i++) {
var pair = queryParams[i].split('=');
if (pair[0] === 'pin') {
pin = decodeURIComponent(pair[1]);
break;
}
}
function makeRequest(url, method, body, callback) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
callback(xhr);
}
};
xhr.send(body);
}
function fetchI18n(then) {
makeRequest('/i18n.json', 'GET', null, function (response) {
if (response.status !== 200) {
alert('Could not load translations. Please reload the page to try again.');
return;
}
try {
i18n = JSON.parse(response.responseText);
} catch (e) {
alert('Could not parse translations. Please reload the page to try again.');
return;
}
if (i18n.dropHint) {
document.getElementById('drop-hint').innerText = i18n.dropHint;
}
then();
});
}
function setButtonVisible(visible) {
document.getElementById('upload-button').style.display = visible ? 'block' : 'none';
document.getElementById('drop-hint').style.display = visible ? 'block' : 'none';
}
function setDragOver(active) {
document.getElementById('content').className = active ? 'drag-over' : '';
}
function isFileDrag(e) {
var types = e.dataTransfer && e.dataTransfer.types;
if (!types) {
return false;
}
for (var i = 0; i < types.length; i++) {
if (types[i] === 'Files') {
return true;
}
}
return false;
}
function getDroppedFiles(dataTransfer) {
var files = [];
var items = dataTransfer.items;
if (items && items.length > 0 && items[0].webkitGetAsEntry) {
for (var i = 0; i < items.length; i++) {
// Directories cannot be read as blobs, so uploading them would fail midway; skip them.
var entry = items[i].webkitGetAsEntry();
if (entry && entry.isDirectory) {
continue;
}
var file = items[i].getAsFile();
if (file) {
files.push(file);
}
}
} else {
for (var i = 0; i < dataTransfer.files.length; i++) {
files.push(dataTransfer.files[i]);
}
}
return files;
}
function showStatus(text, isError) {
var statusText = document.getElementById('status-text');
statusText.style.display = text ? 'block' : 'none';
statusText.className = isError ? 'error' : '';
statusText.innerText = text;
}
function showProgress(finished, total) {
var progressText = document.getElementById('progress-text');
progressText.style.display = 'block';
progressText.innerText = finished + '/' + total + ' file(s)';
}
function hideProgress() {
document.getElementById('progress-text').style.display = 'none';
}
// Ends the upload flow: keeps the error visible and lets the user try again.
function finishWithError(text) {
uploading = false;
showStatus(text, true);
setButtonVisible(true);
}
// Ends the upload flow after all files have been sent.
function finishWithSuccess() {
uploading = false;
showStatus('');
setButtonVisible(true);
}
function startUpload(fileList) {
uploading = true;
setButtonVisible(false);
hideProgress();
showStatus(i18n.waiting);
var selectedFiles = {};
var filesDto = {};
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
var fileId = String(i);
selectedFiles[fileId] = file;
filesDto[fileId] = {
id: fileId,
fileName: file.name,
size: file.size,
fileType: file.type || 'application/octet-stream'
};
}
var body = JSON.stringify({
info: {
alias: 'Web Browser',
version: '2.1',
deviceType: 'web',
fingerprint: getFingerprint(),
port: 0,
protocol: location.protocol === 'https:' ? 'https' : 'http',
download: false
},
files: filesDto
});
prepareUpload(body, selectedFiles, true);
}
function prepareUpload(body, selectedFiles, firstAttempt) {
var url = BASE_URL + '/prepare-upload';
if (pin) {
url += '?pin=' + encodeURIComponent(pin);
}
makeRequest(url, 'POST', body, function (response) {
if (response.status === 401) {
var newPin = prompt(i18n.enterPin + (firstAttempt ? '' : '\n' + i18n.invalidPin));
if (!newPin) {
finishWithError(i18n.invalidPin);
return;
}
pin = newPin;
prepareUpload(body, selectedFiles, false);
return;
}
if (response.status === 429) {
finishWithError(i18n.tooManyAttempts);
return;
}
if (response.status === 403) {
finishWithError(i18n.uploadRejected);
return;
}
if (response.status === 409) {
finishWithError(i18n.busy);
return;
}
if (response.status === 204) {
// The recipient accepted the request but declined every file.
finishWithError(i18n.uploadRejected);
return;
}
if (response.status !== 200) {
finishWithError(errorText(response.status));
return;
}
var data = JSON.parse(response.responseText);
uploadFiles(data.sessionId, data.files, selectedFiles);
});
}
function uploadFiles(sessionId, tokens, selectedFiles) {
var fileIds = getKeys(tokens);
var total = fileIds.length;
showStatus('');
showProgress(0, total);
function uploadNext(index) {
if (index >= total) {
finishWithSuccess();
return;
}
var fileId = fileIds[index];
var url = BASE_URL + '/upload' +
'?sessionId=' + encodeURIComponent(sessionId) +
'&fileId=' + encodeURIComponent(fileId) +
'&token=' + encodeURIComponent(tokens[fileId]);
makeRequest(url, 'POST', selectedFiles[fileId], function (response) {
if (response.status !== 200) {
finishWithError(errorText(response.status));
return;
}
showProgress(index + 1, total);
uploadNext(index + 1);
});
}
uploadNext(0);
}
function getFingerprint() {
var fingerprint = sessionStorage.getItem('fingerprint');
if (!fingerprint) {
fingerprint = 'web-';
for (var i = 0; i < 32; i++) {
fingerprint += Math.floor(Math.random() * 16).toString(16);
}
sessionStorage.setItem('fingerprint', fingerprint);
}
return fingerprint;
}
// A status of 0 means the request never completed (connection lost or aborted by the recipient).
function errorText(status) {
return status ? 'Error: ' + status : 'Error';
}
function getKeys(obj) {
var keys = [];
for (var key in obj) {
keys.push(key);
}
return keys;
}
function init() {
var fileInput = document.getElementById('file-input');
document.getElementById('upload-button').onclick = function () {
fileInput.click();
};
fileInput.onchange = function () {
if (fileInput.files.length > 0) {
startUpload(fileInput.files);
// Reset the input so selecting the same files again fires this event.
fileInput.value = '';
}
};
document.addEventListener('dragenter', function (e) {
if (!isFileDrag(e)) {
return;
}
e.preventDefault();
if (uploading) {
return;
}
dragDepth++;
setDragOver(true);
});
document.addEventListener('dragover', function (e) {
if (!isFileDrag(e)) {
return;
}
// Prevent the browser from opening the file; during an upload, show that dropping is not possible.
e.preventDefault();
if (uploading) {
e.dataTransfer.dropEffect = 'none';
}
});
document.addEventListener('dragleave', function (e) {
if (!isFileDrag(e) || dragDepth === 0) {
return;
}
dragDepth--;
if (dragDepth === 0) {
setDragOver(false);
}
});
document.addEventListener('drop', function (e) {
if (!isFileDrag(e)) {
return;
}
e.preventDefault();
dragDepth = 0;
setDragOver(false);
if (uploading) {
return;
}
var files = getDroppedFiles(e.dataTransfer);
if (files.length > 0) {
startUpload(files);
}
});
fetchI18n(function () {});
}
init();
</script>
</body>
</html>