How to link to download html file?

Asked

Viewed 1,943 times

0

I’m using the tag:

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

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

http://127.0.0.1:8000/172.23.5.17/Images/etc...

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 value.download:

\\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:

http://127.0.0.1:8000/172.23.5.17/Images/Landsat/IMAGENS_LANDSAT_8_2017/Processed/003_068l8_oli_002065_20140803_b654_fuseo_sirgas2000_utm.tiff

  • In: <a href="result.download" download> read <a href="{{ result.download }}" download>

2 answers

3

Neither Django, nor any maintained Python framework has an automatic match between file paths on the server and Urls - this is something of static HTML that comes from the pre-1.0 Web - and it’s something that happens in CGI applications, or in technologies like PHP or Asp.

What you need to put on href from your page is the address for a view, like any other view in Django. The code of that view is going to return, instead of HTML, an image file, which will be 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 the arch on the disk, although this is bad security practice. The technologies that do this by default have led years old to close all (if there isn’t any left) security holes derived from this (for example, the user when placing directories under the name "." in the URL could access any file visible by the HTTP server process on the server - this trivial vulnerability persisted for years in some technologies).

Of course it is possible to 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 directory structure /static/... - and as this is done by the framework, the possible gaps are already well resolved.

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

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

  • On the other hand, servicing the file statically, without having to go through Python, overloads the server less. Could use rewriting of Urls to avoid exposing the real path on disk.

  • 1

    I updated the answer. It’s not like on a modern wsgi server this "Python pass" will weigh anything. A Jango with production configuration should easily reach 3000req/s without needing to optimize anything - it would need to be serving too much image to make any difference in server load. However, static Urls can be cached into several different layers of the structure, including Cdns, so yes - static Urls have their advantage, if the same images are served to multiple users.

0

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

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

(Django admin, Static and media files)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.