How to make link to file download in html?

0

I'm using the tag:

<a href:"{{ arquivo.caminho }}" download>link</a>

With the download attribute to download links from my local network files (tiff images), however, the downloaded file is a .tiff file containing the html of my page. I also noticed that the donwload link returned is prefixed by the address of my django server:

link ...

My html code:

<tr>
  <td id="td_result"><p class="resultados_p">{{ resultado.nome }}</p></td>

  <td>
    <a href="resultado.download" download>
      <div class="download"></div>
    </a>
  </td>
</tr>

result.download value:

  

\\ 172.23.5.17 \ Images \ landsat \ IMAGENS_LANDSAT_8_2017 \ Processed \ 003_068l8_oli_002065_20140803_b654_fuseo_sirgas2000_utm.tiff

(I've tried changing the bars)

Link returned by page:

  

    
asked by anonymous 30.01.2018 / 19:56

2 answers

3

Neither Django nor any maintained Python framework has an automatic match between file paths on the server and URLs - this is some static HTML coming from the pre-1.0 Web - and it's something that happens in CGI applications, or in technologies such as PHP or Asp.

What you need for the href of your page is the address for a view, just like any other view in Django. The code of this view will return, instead of HTML, an image file, which is marked as such in HTTP headers. (The framework does this automatically).

Within the Python code of this view, you can choose to match the path that comes in the URL directly to files on the disk, although this is a bad security practice. Technologies that do this by default took years to close all (if any) security flaws derived from this (for example, the user putting directories with the name ".." in URL could access any file visible by the HTTP server process on the server - this trivial vulnerability has persisted for years in some technologies.)

Of course you can put your files in the configured folder to serve static resources - below it there is a match between the path passed in the URL and the /static/... directory structure - and as this is done by the framework, the possible loopholes are already well resolved.

But most normal in these cases is having an image generated by the application, from dynamic form - either at each access, or a graph generated at each time interval, etc ... in this case, enter the idea of the view - The serve(request, caminho_do_arquivo) function can be used in your view to serve a file directly (import it with from django.contrib.staticfiles.views import serve ).

The file path can be associated with an image ID in the database - and never be exposed in the URL if you prefer

    
30.01.2018 / 21:56
0

Dude, being arquivo the object of your model in Django ORM and caminho the attribute ImageField or something of the type, you can access the url direct like this:

<a href="{{ arquivo.caminho.url }}" target="_blank">link</a>

( Django admin, static and media files )

    
30.01.2018 / 22:21