简体   繁体   中英

Forcing browser to open “Save As” dialog on file download

I have read at least 40-50 posts on this topic and none of the solutions really do what i want. I 'command' my WinCE 6.0 board to create a backup file through AJAX request and then i want to save this file to local WS.

I have the following javascript code that is called once the AJAX request completes:

function DownloadControllerBackupDB(theResult)
{
   if(typeof(theResult.Filename) != undefined)
   {
      var myFileLocation = '/data/' + theResult.Filename;

      location.href = myFileLocation;
   }
   else
   {
      Error("Backup failed !");
   }
}

I also tried this approach (instead of the 'location.href' technique):

function SaveToDisk(fileURL, fileName) 
{
    var save = document.createElement('a');

    save.href = fileURL;
    save.target = '_blank';
    save.download = fileName || 'unknown';

    var event = document.createEvent('Event');
    event.initEvent('click', true, true);
    save.dispatchEvent(event);
    (window.URL || window.webkitURL).revokeObjectURL(save.href);
}

Both 'techniques' endup @ the same result in chrome 50, the file is indeed saved to disk in the local folder dedicated to user's file download...

I would want the browser to ask me (with a file requester) before saving this file to disk so i can decide where to put it.

I read somewhere that you can set an option in chrome (and probably other browsers) to force it asking before saving files but i think this is unacceptable to ask a user to do (due to the complexity of the operation).

Is there anyway to 'Force' the browser to ask before saving a linked file ?

Thanxs

   let a = document.createElement('a');
   a.href = 'https://www.google.ru/logos/doodles/2016/2016-hockey-world-championship-5085766322487296.2-hp.jpg';
   a.download = true;
   var event = document.createEvent('Event');
   event.initEvent('click', true, true);
   a.dispatchEvent(event)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM