I am using Django to create a very very simple app that allows a client to post images through normal requests. However, the user who views the actual web page, has Javascript running in it that performs continuous AJAX request to look for the next picture posted by the other client, but view them Locally on the django server. i.e. local client opens up page, js continuously submits ajax posts looking for pic number 1. When the remote client posts pic number 1, local client sees pic number 1 in the web page, then asks the server continuously for pic number 2... and so on.
Here is the view that responds to the local client's ajax:
class ViewPictures(View): def post(self,request): #look for ajax request if request.is_ajax(): req_post = request.POST #the pk of the picture the client is looking for num_pic = req_post["pk"] #get the picture from SQL try: picture = Picture.objects.get(pk=num_pic) except Picture.DoesNotExist: data = simplejson.dumps({'picture':0}) return HttpResponse(data,content_type="application/json") #get the local path of the pic path = picture.photo.file #Serialize pathname response_data = simplejson.dumps({'picture':str(path)}) #response with path name JSON return HttpResponse(response_data,content_type="application/json") else: #else forbidden request return HttpResponseForbidden() Here is the local client's client-side page:
function get_pic(pic_num){ //ajax request for pic $.ajax({ url:'{% url "viewpics"%}', method: 'POST', async: true, data: {'pk':pic_num}, success: function(response){ //if pic not none if(response.picture!=0){ var image = '<img id="image" src='+response.picture+ ' height="42" width="42" />'; $(".image_show").prepend(image); pic_num++; } get_pic(pic_num); }, error: function(jqXHR, textStatus, errorThrown){ alert(errorThrown); } }); }; $(document).ready(get_pic(1)); </script> <head> <title>IMAGE VIEWER</title> <div class ="image_show"> </div> </head> I read that since the server is on the same machine as the client fetching the pics,it just makes sense to send the path of the picture on the local machine to the client. However, I also read that browsers don't allow local access of files into html pages. I was wondering if there is a method for allowing the client to view the local imgs in their web page, or is it better to serialize the local imgs and send them to the client? Is there a better way to do this, should it be done with both clients being remote?