简体   繁体   中英

Start a file download after redirecting a user to another page (NodeJS / Express)

Background Information

  • I am working on a NodeJS w/ Express project.
  • There are publicly visible buttons that should allow file downloads, and these buttons have protected routes which redirect the user to a login page if they are not logged in.
  • I am storing some information related to file downloads in a cookie, and that is working. Essentially it is need_to_download_file2 == true

The Problem

Once the user authenticates, I want to redirect them to the home page and then start a file download for the file they originally clicked on. As of now, everything is working, but the file download does not start after the redirect, the user must click download again to initiate it.

Main Approach:

  • I am attempting to call window.open('/download/file2') in the index.html page's onLoad() event. However, the network tab shows the request as being cancelled. This is all that is contained in the onLoad() event.
// onLoad
function hidePreloader () {
  document.getElementById('preloader').style.display = 'none'
  document.getElementById('contentDiv').style.display = 'block'

  window.open('localhost:3000/download/file2','_self');
}

I would really appreciate any help, and can provide more information if needed!

Thanks in advance.

If I recall properly, all client upload files must be picked by the user themselves. A server is not allowed to specify a file to upload on its own (security problem with that as a server could randomly target any file on your hard drive). Instead, the user has to manually select the file to upload.

What you could do (for a better user experience) is check to see if the user appears to be authenticated on the client side via Javascript. If not, then use Ajax to log them in (without changing/losing the current page). Then, after they've authenticated via Ajax, the original upload file is still specified in the current page and it can then be started without the user having to pick the file again.

Your server will still check for proper auth when the upload request arrives, but if the user follows this path, they should be authenticated already.

FYI, window.open() is often blocked by the popup blocker functionality in a browser if not the direct result of a user click (and even then it's still sometimes problematic). It would be more reliable to not rely on popup windows. Often times, you can simulate the same experience with a popup built into the page, not as a separate window.

Add trigger and fire it which will call the same method , which is called by button. It's easy to implement in JQuery but in pure javascript How to trigger event in JavaScript? may help you. In node.js/express for frontend you can install jqyery module by npm i jquery .

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