2016-09-15 9:21 GMT+02:00 Rahul Doshi <rauljustdance@gmail.com>:
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 src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/ "></script>jquery.min.js <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.pyfrom django.shortcuts import renderfrom django.http import HttpResponsefrom django.shortcuts import render_to_responsefrom polls.models import Filesfrom os import walkfrom os.path import isfile, joindef 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})
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-users@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
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-users@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/CALXYUbkBGy8n%2Bb4Gdh1VwH7L-Xo1cH4VxcDRBmyUYCS00qLFjw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment