brunoid brunoid - 1 year ago 150
Ajax Question

Javascript prevent file download by direct url

I want to prevent file download from website by direct URL.

This way I need to allow file download only when a some custom parameter is set along with a client request.

Is it possible via JavaScript to set for example custom HTTP request header value when user clicks the HTML link for file download ? Based on presence of this parameter server side logic will decide - allow file download or no.

If it is not possible what other approaches can be used in order to prevent file download by direct URL ?

Answer Source

From what I understand, your question is "Can I add a custom header to a request using Javascript?".

If you are using ajax to do your request, it's pretty simple. Here's an example:

// Request with custom header
    url: 'foo/bar',
    headers: { 'x-my-custom-header': 'some value' }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download