2

pdf.js in in my static files(as shown below):
/static/js/pdf.js

And i am able to display a pdf located in static folder(as shown below):
/static/sample-pdf.pdf

but i want to be able to show a pdf located in my media folder(as a pdf should be stored as a media file in django)

media folder is in same directory as of static folder(as shown below):
project/static and
project/media

Is it possible ?

Do ask if more clarity is required.

dreamer
  • 901
  • 2
  • 15
  • 38
  • Just to be clear, from project/static/pdf.js, you want to access a PDF in your project/media folder? Can't you just do `../media/sample-pdf.pdf`? – SilentDev Oct 22 '15 at 19:41
  • if target pdf is in the `media` folder, then how can i do a `../static/sample-pdf.pdf` ?????? – dreamer Oct 22 '15 at 19:44
  • Able to do this `static/js/viewer.html/?file=/static/sample-pdf.pdf` but not able to do this `static/js/viewer.html/?file=/media/sample-pdf.pdf` – dreamer Oct 22 '15 at 19:49
  • i assume it cant access anything outside of the url that the javascript file came from......... is tat ryt ? – dreamer Oct 22 '15 at 19:51

2 Answers2

1

Try setting your MEDIA_ROOT and MEDIA_URL in your settings.py and see if it works. Open up settings.py and add this:

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
# MEDIA_ROOT is used when users upload media.

MEDIA_ROOT = '/home/absolute/path/to/your/media/folder'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

Then, you may also need to add this in your URLs.py:

from . import settings
import django.views.static

url(r'^(.*?)media/(?P<path>.*)$', django.views.static.serve,
             {'document_root': settings.MEDIA_ROOT}),
Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • can u explain the URLs.py snippet ? – dreamer Oct 23 '15 at 03:42
  • @dreamer From my understanding, just like how you have a static folder which you can access with the `static/` URL, the snippet above allows you to access `settings.MEDIA_ROOT` (your media folder) by going to `media/` URL. With this code, you can also allow users to upload files (which will be saved in your media folder). To access these media files, you go to `media/FileName`. Django captures `fileName` and passes it to `django.views.static.serve` which handles it (in similar ways as how `static` is handled). – SilentDev Oct 23 '15 at 04:42
  • @dreamer I got the URLs.py snippet from here: http://stackoverflow.com/questions/5517950/django-media-url-and-media-root and it is also talked about a bit in the documentation here: https://docs.djangoproject.com/en/1.8/howto/static-files/#serving-files-uploaded-by-a-user-during-development Keep in mind though that it is for development use and for your production server, you may need to do it a bit differently (as mentioned in the documentation). – SilentDev Oct 23 '15 at 04:44
  • so u r telling, the above snippet will provide the files at `127.0.0.1:8000/static/js/pdf.js/web/viewer.html?file=/media/sample-pdf.pdf` – dreamer Oct 23 '15 at 04:55
  • @dreamer From my understanding, it should. Try it and let me know if you get any errors (and what the errors are). – SilentDev Oct 23 '15 at 19:35
  • i used the url provided by dreamer, doesn't quite work out. – Diansheng Nov 14 '17 at 07:21
1

This will display your file in the html page from the directory

<iframe src="../media/sample-pdf.pdf" id = "pdf" style="width:1500px; height:1500px;" frameborder="0"></iframe>

check console of browser to check the path is right if its not showing. Its that much simple

Shinto Joseph
  • 2,809
  • 27
  • 25