0

I am using this code to download a DataURL to a file

    function downloadURI(uri, name) {
        var link = document.createElement("a");
        link.download = name;
        link.href = uri;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        delete link;
      }

      downloadURI(data, "images/helloWorld.png");

but it tries to download to user storage. I want to download to server storage (For example download to "images/download.png" rather than prompt the user to download on their computer.

I tried to pass into PHP but the dataurl is too large for ajax to pass through.

connorjrt
  • 7
  • 5
  • `delete link` doesn’t really do anything. Did you mean `link.remove()`? – Sebastian Simon Oct 10 '21 at 09:56
  • This is just a copy paste of my code. It works fine though. Just not sure on how to get it to download to the server rather than prompting the user – connorjrt Oct 10 '21 at 10:33
  • Are you trying to pass base64 dataurl through ajax and its not working? **Theres no way for you to save anything to the server unless its done through PHP, javascript cant do that alone** – slashroot Oct 10 '21 at 11:02
  • Is there a way to pass the dataurl to the PHP then? Right now I get 414 (request-uri too long) – connorjrt Oct 10 '21 at 11:03
  • @Devfine here check this S.O. post that should help you out with passing base64 data through ajax: [How to post an image in base64 encoding via ajax](https://stackoverflow.com/questions/34047648/how-to-post-an-image-in-base64-encoding-via-ajax) – slashroot Oct 10 '21 at 11:21
  • I modified the code to be ` $.ajax({ url: 'register-database.php?name="blank"&score=1', type: 'POST', processData: false, contentType: 'application/octet-stream', data: makeblob(data) }) .done(function(data) {alert("success");}) .fail(function() {alert("error");});` and it returns success but doesn't actually add it to the database, I don't know where it's going wrong – connorjrt Oct 10 '21 at 11:27
  • @Devfine Have you copied/created the `makeblob()` function as well? And if it returns success, maybe check your PHP `register-database.php` file to make sure your code runs as it should on that file. – slashroot Oct 10 '21 at 11:32
  • I've copied the function, the code there works normally. I have modified the Ajax now to say "data: {name: "blank", imPath: makeblob(data), score: 1}," and I'm using $_GET["name"] (etc...) to get the data in the php – connorjrt Oct 10 '21 at 11:35
  • Since your passing the query string in your ajax URL you wont need to add those to the data fields. Youll be able to use $_GET['name'] without passing it in data. And then use $_POST['imPath'] for your image data. Now on your PHP file you will need to undo what the BLOB function did by using base64 encode, so that you can read your data and save it. – slashroot Oct 10 '21 at 11:58
  • Still nothing in the database. I'm now using `data: makeblob(data),` and ` url: 'register-database.php?name=None&score=1',` and `VALUES (".$_GET["name"].", '".$_POST["imPath"]."', '".$_GET["score"]."')";` in the PHP – connorjrt Oct 10 '21 at 12:07
  • @Devfine And whats the content of `$_POST['imPath']` on your PHP file? Is your database query running and field `$_POST['imPath']` is empty or is your db query not running at all? – slashroot Oct 10 '21 at 12:19
  • Well I'm passing in makeblob(data), the value of data is a DataURL like "data:image/png;base64,iVBORw0KGgoAAAAN..." – connorjrt Oct 10 '21 at 13:27
  • It now creates a database query but "imPath" reamins blank. The javascript code is ` url: 'register-database.php?name=Blank&score=1', type: 'POST', processData: false, contentType: 'application/octet-stream', data: "test", ` and the PHP is ` $name = $_GET["name"]; $score= $_GET["score"]; $path = $_POST["imPath"]; $sql = "INSERT INTO People (`Name`, `ImagePath`, `Score`) VALUES ('".$name."','".$path."',".$score.")";` – connorjrt Oct 10 '21 at 13:53

1 Answers1

0

First of all, you need to create some backend code to download it on server storage. You might only need the URI from frontend, make a call on some backend API and then request everything from backend.

ezhupa99
  • 1,838
  • 13
  • 18
  • How am I supposed to get the URI from the backend? It seems too large for PHP to process – connorjrt Oct 10 '21 at 10:54
  • You get it by making a request from Front-End like [this](https://stackoverflow.com/a/58437909/8665500) put your URI and Name params on req parameters and process everything on PHP – ezhupa99 Oct 10 '21 at 12:03