A public cloud with django backend

40 views
Skip to first unread message

Rahul Doshi

unread,
Sep 15, 2016, 6:53:55 AM9/15/16
to Django users
Hi ,I want to setup  a dropbox like server with django. So far i have achieved uploading files onto a location .I want these files to show up on browser which they do(while saving file to the location I indexed an entry in the Db so i just print the file names from the DB(POSTGRESQL).Now i want to provide users options to view or download the files.I put a link on it and tried ,it did not work since it shows an javascript error "NOT ALLOWED TO LOAD LOCAL RECOURCE" . All uploaded files are loaded in C drive of my laptop. Where else do i load so that javascript can load it? Attaching my code ..thanks in advance.

Index.html

<script>
function downloadFile(filename){


}
</script>
<table id='filetable' border = '1'>
{% for i in q  %}
   <tr>
   <td>{{i.id}}</td>
   <td>
   <a href='C:\Users\rdoshi\storage\{{i.file_name}}'>{{i.file_name}}</a>
   <button type="button" onclick="downloadFile("{{i.file_name}}")">Download</button>
   </td>
   </tr>
 {% endfor %}
</table>
<form method = "post" action="../upload/" enctype ="multipart/form-data">{% csrf_token %}
         <input type="file" name="files" multiple />
<input type = "submit" value="Upload" />
</form>

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render_to_response
from polls.models import Files
from os import walk
from os.path import isfile, join
def index(request):
return render(request,"index.html", {})

def upload(request):
for x in request.FILES.getlist("files"):
def process(f):
with open(r'C:\Users\rdoshi\storage\%s ' %f.name , 'wb+') as destination:
b = Files(file_name= f.name)
b.save()
for chunk in f.chunks():
destination.write(chunk)
process(x)
q = Files.objects.all()
return render(request,  "index.html", {'q' : q})


Andreas Kuhne

unread,
Sep 16, 2016, 7:11:03 AM9/16/16
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b6396853-ae8f-4fef-ac91-7ab235eb22b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi, 

I think you should look at how the django storage system works https://docs.djangoproject.com/el/1.10/ref/files/storage/.

You should store the files in the media directory of your app and not directly on your harddrive in an arbitrary folder. Then you can serve the files by using the url that you get from the file field in the model.

The way you have created it now the link goes to your local harddrive and not to the server itself. If you where to run that on the internet, the files would be located on the servers drive (to wherever you put the file folder) and then when clicking on the link the users would be looking at there own harddrive for the file.

Check https://docs.djangoproject.com/ja/1.10/ref/models/fields/#filefield for more information about the filefield.

Regards,

Andréas

Tom Evans

unread,
Sep 19, 2016, 11:00:33 AM9/19/16
to django...@googlegroups.com
On Thu, Sep 15, 2016 at 8:21 AM, Rahul Doshi <raulju...@gmail.com> wrote:
> Hi ,I want to setup a dropbox like server with django. So far i have
> achieved uploading files onto a location .I want these files to show up on
> browser which they do(while saving file to the location I indexed an entry
> in the Db so i just print the file names from the DB(POSTGRESQL).Now i want
> to provide users options to view or download the files.I put a link on it
> and tried ,it did not work since it shows an javascript error "NOT ALLOWED
> TO LOAD LOCAL RECOURCE" . All uploaded files are loaded in C drive of my
> laptop. Where else do i load so that javascript can load it? Attaching my
> code ..thanks in advance.


For security reasons, browser do not allow internet zone sites to
access local content from javascript (otherwise, any site could read
any accessible file from your computer and submit it, well, anywhere).

Serve the files from the webserver instead.

Cheers

Tom

Phang Mulianto

unread,
Sep 20, 2016, 11:12:30 AM9/20/16
to django-users
hi,
this one :

<a href='C:\Users\rdoshi\storage\{{i.file_name}}'>{{i.file_name}}</a>   

should be 

<a href='/media/{{i.file_name}}'>{{i.file_name}}</a>

it will point to your local webserver if in local dev (http://127.0.0.1/media/filename) , and in prod will be from your domain (http://yourdomain.com/media/filename)

Cheers,

Mulianto

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages