12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
- alert('This website requires HTML5, which your browser does not fully support. Please use a supported browser to use this website.');
- }
- var peer = new Peer({host: 'localhost', port: 9000, path: '/node_modules/peerjs'});
- function send() {
-
- var file_to_send = document.getElementById("file_to_send").files[0];
- var dest_id = document.getElementById("dest_id").value;
- if(!file_to_send && !dest_id) {
- alert("Please select a file and previde a Destination ID.");
- }
- else if(!file_to_send) {
- alert("Please select a file.");
- }
- else if(!dest_id) {
- alert("Please provide a Destination ID.");
- }
- else if (dest_id === peer.id) {
- alert("You can't send a file to yourself!");
- }
- else {
-
- var blob = new Blob([file_to_send], {type: file_to_send.type});
-
- var con = peer.connect(dest_id, { label: 'file', reliable: true});
- con.on('open', function() {
-
- con.send({
- name: document.getElementById("file_to_send").files[0].name,
- file: blob,
- type: document.getElementById("file_to_send").files[0].type
- });
- });
- }
- }
- peer.on('open', function(id) {
- document.getElementById("my_id").textContent = peer.id;
- });
- peer.on('connection', function(conn) {
- conn.on('open', function() {
- conn.on('data', function(data) {
-
-
-
- var blob = new Blob([data.file], {type: data.type});
- var url = window.URL.createObjectURL(blob);
-
- var table = document.getElementById("file_table");
-
- var elem = document.getElementById("empty_row");
- if(elem) {
- elem.parentNode.removeChild(elem);
-
-
-
-
-
-
-
-
- }
- var row = table.insertRow(1);
- var a = document.createElement("a");
- a.href = url;
- a.innerHTML = data.name;
- a.setAttribute("download", data.name);
- var file_col = row.insertCell(0);
- file_col.appendChild(a);
-
-
-
- });
- });
- });
- peer.on('error', function(err) {
- if (err.type === 'peer-unavailable') {
- alert("No user with the given ID is currently connected.");
- }
- });
|