AAhmed AAhmed - 29 days ago 13
HTML Question

can a file be downloaded from django development server

I am developing a simple website, where users can input some data through a form and by using the data some work will be done which will generate some image and text files in a folder in server. Now, during the submission of the form the user will be provided with a job id and the resulting folder path would be annotated with the same id. In a search box user will be able to download their results by id.

The problem is, how to provide those resulting files to the user to download?
I have used the following code in my html to make the download link:

{% extends "personal/header.html" %}
{% block content %}
<h4>Here is the result for you</h4>
<h4>Last name: {{ resultRow.last_name }}</h4>

<hr>
<h4>path: {{ complementRow.path_for_folder }}</h4>
<a href='C:\Users\path\to\heatmap.png' download="img">Heatmap</a>

{% endblock %}


By doing this, the download link appears in the page of django development server but clicking the link does not download the file and shows "Failed: Network error".

So, the question is, is the error because of development server or am I doing something wrong? If I am wrong please suggest me right way.
Note: I will use "complementRow.path_for_folder" for specifying the file. Here I have used an instantiation of the variable.

I can post viwes.py file or whatever needed to understand

Answer

It is because the url in the <a/> is invalid

<a href='C:\Users\path\to\heatmap.png' download="img">Heatmap</a>

On the user's browser, this path C:\Users\path\to\heatmap.png is telling the browser to load a file from C:\Users\path\to in the user's local harddisk. It is not what you intended

You need to make the heatmap.png available from a server (either your dev server or other hosting service). Then adjust the url accordingly.

Since you want to serve static file from the django server, you can take a look at this documentation: https://docs.djangoproject.com/en/1.10/howto/static-files/

Comments