0

I want to download a file from the local directory when the user clicks on a particular link. How I can do it using python.

harsh
  • 23
  • 6
  • "when user clicks on perticular link it goes to correct location but file does not download" what does happen? what type of file? – atmd Feb 14 '19 at 04:06
  • Possible duplicate of [starting file download with JavaScript](https://stackoverflow.com/questions/365777/starting-file-download-with-javascript) – atmd Feb 14 '19 at 04:07
  • I have used whoosh library for indexing . I have around 900 files which are stored in static>datasets>documents directory. I get error as - The requested URL /static/documents/dataset/dynamic.ppt was not found on this server. – harsh Feb 14 '19 at 04:11
  • so it doesn't go to the correct file, looks like you found the issue then – atmd Feb 14 '19 at 04:13
  • The path I found is correct but it does not download. – harsh Feb 14 '19 at 04:14
  • if you have the error "The requested URL /static/documents/dataset/dynamic.ppt was not found on this server." then how can the file path be correct? where is that error coming from? – atmd Feb 14 '19 at 04:17
  • see my files are stored in this location - static/documents/dataset/dynamic.ppt and error is coming as The requested URL /static/documents/dataset/dynamic.ppt was not found. if you observe here both path are same. – harsh Feb 14 '19 at 04:18
  • its unclear what your asking, it certainly doesn't seem to be a javascript problem – atmd Feb 14 '19 at 04:24

3 Answers3

0

Try this on click of link:

location.replace('your_loacal_filepath')

For Example:

location.replace('/files/out.txt')
0

if you are rendering HTML, then you should read about the HTML download Attribute

Quoting

The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.

This attribute is only used if the href attribute is set.

else if(currentobj.filename != null){
   var xy = '{% static "/documents/dataset/" %}' + currentobj.filename;
   alert(xy);
   var $tr = $('<tr><td style="padding:5px;"><a href="'+ xy +'" 
                 target="_blank" download>' + currentobj.filename.slice(0,25) + 
                 "....." +'</a></td> </tr>');
   $tbl.append($tr);
  }

just add download tag along the hyperlink

if you want to specify a specific name for the download file then just mention it like this

<a href="/images/myw3schoolsimage.jpg" download="w3logo">
Dabees
  • 494
  • 4
  • 14
  • I have 900 files then how I can use download="w3logo" – harsh Feb 14 '19 at 04:15
  • can you explain please if there is error message or any clue ?? – Dabees Feb 14 '19 at 04:18
  • I m working on chatbot where I got multiple results when user click on perticular link it will be downloaded. I have two functions , first function I have given server link that will work . but it does not work for local directory . I got error as -The requested URL static/documents/dataset/dynamic.ppt was not found but file path is correct. – harsh Feb 14 '19 at 04:21
  • did you try to substitute the link in the `href` tag with an online url to a file, instead of using local one ?? and see what happens ...like for example use this photo and see the errors that is going to appear `https://www.w3schools.com/images/myw3schoolsimage.jpg` – Dabees Feb 14 '19 at 04:25
  • I have tried instead of local directory when I pass server url it works. – harsh Feb 14 '19 at 04:28
0

You can try this. The download attribute instructs browsers to download a URL instead of navigating to it. Check out. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes

himank
  • 439
  • 3
  • 5