PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 15, 2022

[FIXED] How to specify download location in Html using JavaScript

 August 15, 2022     html, javascript, output     No comments   

Issue

is it possible to specify the download location for a file from a Html page using JavaScript?

the function I am currently using to down my file is provided below.

function saveTextAsFile()
{      
    var textToWrite = document.getElementById("inputTextToSave").value;
    var textFileAsBlob = new Blob([textToWrite], {type:'xlsx'});
    var fileNameToSaveAs = "NAME.csv";
    var downloadLink = document.createElement("a");
    downloadLink.download = fileNameToSaveAs;
    window.URL = window.URL || window.webkitURL;
    downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
    downloadLink.onclick = destroyClickedElement;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);
    downloadLink.click();
}

Thanks for your help in advance.


Solution

Whether the browser asks the user or not is down to the preferences in the browser.

You can't bypass those preference, otherwise it would violate user's security.

What you can do is make sure you're sending the browser as much information as possible to help it make for a good user experience. If you're not already doing so, be sure to include a Content-Disposition header in the response to the download request, including a filename field:

Content-Disposition: attachment; filename=test.csv

Also see these other stackoverflow questions:
  Specify default download folder - possibly with JavaScript?
  Set file download destination using ExtJs



Answered By - Darren Willows
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing