0

I wanted to share something that i found interesting and hopefully get an explanation of this behavior. It goes like this. I created a page and all it does is upload a file to the server.

const input = document.getElementById("input");
const button = document.getElementById("button");

button.addEventListener("click", () => {
  const file = input.files[0];

  const xhr = new XMLHttpRequest();
  xhr.open("POST", "http://localhost:4000");

  const formData = new FormData();
  formData.append(input.id, file);

  xhr.onload = () => {
    console.log(xhr.response);
  };
  xhr.send(formData);
});

server

const http = require("http");
const express = require("express");
const app = express();
const upload = require("express-fileupload");
const cors = require("cors");
const fs = require("fs");

app.use(cors());
app.use(upload());

const server = http.createServer(app).listen(4000);

app.use("/", express.static(__dirname + "/client"));

app.post("/", (req, res) => {

  fs.appendFile("./uploads/" + req.files.input.name, req.files.input.data,
    () => {
      res.send("file uploaded");
    }
  );
});

On pc it works just fine. Out of curiosity i wanted to test this on my android device. My pc is connected to mobile hotspot, and i go cmd>ipconfig to get ip adress. Now i enter the page on my phone and when i press upload nothing happens, file not added to folder on server. I think maybe onclick event listener doesn't work on android so i test it by removing http request and changing background color on press. It works. I decide to use USB debbuging so i can get to chrome dev tools on android. And then i try uploading again.

enter image description here

This time it works !
I think maybe it has something to do with phone now being connected to pc by usb. I exit android chrome dev tools and try uploading again, with usb connection. Again it does NOT work, file not added to folder on server.
Does anyone have an idea why ONLY when using android dev tools does this work?

Zobla
  • 123
  • 2
  • 7

0 Answers0