How to upload a file to a webserver with its name in the url

265
August 19, 2017, at 05:56 AM

I want to upload an image from my phone via an app to the server.

Using PHP I always have to submit a form but I can't, since I just call open a link: http://xxx.yy/?file=C:/xxx/yyy/zzz.png

Do I have any other possibilities to upload an image from an android application to a webserver?

How do I upload the file with the filepath given in the URL?

Answer 1

You can request the file from local filesystem as a Blob using XMLHttpRequest() or fetch() utilize FileReader to convert the Blob to a data URI, set the .href property of an <a> element to path to server with query string where data URI is value of query, call .click() on <a> element. At server process query string parameters at GET request to process data URI representation of file.

const a = document.createElement("a");
document.body.appendChild(a);
const reader = new FileReader;
reader.onload = () => {
  a.href = `/path/to/server?file=${reader.result}`;
  a.click();
}
fetch("/path/to/local/file")
.then(response => response.blob())
.then(blob => reader.readAsDataURL(blob))
.catch(err => console.error(err));
READ ALSO
how to make array of array by looping result from a query

how to make array of array by looping result from a query

I have a survey about games, and I have one table for the games data, and another for people's answers

132
openssl_encrypt VS mcrypt_encrypt

openssl_encrypt VS mcrypt_encrypt

I have to do AES 128 encryption in CBC mode and match it with the same AES encyption in coldfusion

266
How can I execute a method with sockets for PHP [on hold]

How can I execute a method with sockets for PHP [on hold]

I have no problems creating the server and the clientMy question is how to execute a method using sockets, sending the request from the client

152
One page app doesn&#39;t refresh with Ajax and SQL requests (online only)

One page app doesn't refresh with Ajax and SQL requests (online only)

I'm trying to build a trip organiserIt's a one page app

117