0

I want to use mod - xsendfile (which I've downloaded and installed) to save content from urls, external pages, that I read in with urllib and urllib2 in the variable one_download.I'm new to this and not sure how to properly configure some of the x-sendfile properties. In the code below I assume that I can place the urllib content in one_download directly into xsendfile instead of taking a middle step as saving it to a txt file and then pass that txt - file to xsendfile.

import urllib2,urllib
def download_from_external_url(request):
    post_data = [('name','Dave'),]  
    # example url  
    #url = http://www.expressen.se/kronikorer/k-g-bergstrom/sexpartiuppgorelsen-rackte-inte--det-star-klart-nu/ - for example
    result = urllib2.urlopen(url, urllib.urlencode(post_data))

    print result
    one_download = result.read()
    # testprint content in one_download in shell 
     print one_download 



# pass content in one_download, in dict c,  to xsendfile
    c = {"one_download":one_download}
    c['Content-Disposition']= 'attachment; one_download=%s' %smart_str(one_download)
    c["X-Sendfile"] = one_download # <-- not working 

    return HttpResponse(json.dumps(c),'one_download_index.html', mimetype='application/force-download')
user1749431
  • 503
  • 5
  • 21

1 Answers1

0

That's not what X-Sendfile is for; it's for serving static files you already have on disk without having to go through Django. Since you're downloading the file dynamically, and it's in memory anyway, you might as well serve it directly.

Daniel Roseman
  • 541,889
  • 55
  • 754
  • 786
  • I thought so but then I saw some similar solutions. I want to use Tastypie for the downloaded material from a url download when saved in db so I thought of saving it with xsendfile into the db and then use it with the Tastypie webbservice. I could save the urllib download to a txt file and then read it in to db with xsendfile? or perhaphs it's as simple as saving it into a db class field directly ? – user1749431 Nov 06 '15 at 09:49
  • Well, now that makes even less sense. What do you mean by "read it into the db with xsendfile"? xsendfile is from sending to the client, it has nothing to do with "reading into the db". – Daniel Roseman Nov 06 '15 at 10:19
  • Solved by saving the content directly as you suggested, thanks. – user1749431 Nov 06 '15 at 11:13
  • regarding "read it into the db with xsendfile" I tried following the following SO post with the urllib and also another example with urllib but I realize that I've complicated my thinking about this, I was primarily concerned with if there would be a formatting error if saving the content from the url straight off into the db which is why I tried using xsendfile http://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files – user1749431 Nov 06 '15 at 11:39