Reading the input file from the server and displaying the output on the client side

I need to read the file from the server side. Got its contents listed, and sent it to the template Now, my question is how to access this list in order to display the contents of files line by line. I use ajax and jquery to get client-side data.

def showfiledata(request):      
    f = open("/home/tazim/webexample/test.txt")  
    list = f.readlines()  
    return_dict = {'list':list}  
    json = simplejson.dumps(list)  
    return HttpResponse(json,mimetype="application/json")  
+3
source share
4 answers

I would do it like this:

  var target = $('p.target');
  $.getJSON("filedata.json", function(json){
     $.each(json.list, function (i, line) {
       $('<span></span>')
         .html(line)
         .appendTo(target);
  });
+1
source

If your “server file” is HTML (say NEWS.html) and you want to use jQuery ...

to do

<div id="NailMeHere"><!-- filled by Ajax call --></div>

in your document and execute the following code when loading the page

$(document).ready(function(){
    $('#NailMeHere').load("NEWS.html");
});

NEWS.html CSS, , ....

+1

HTTP jQuery.ajax().

, list , json JavaScript ajax.

JSON, , FireBug, console.log();.

, , .

({some:'obj'}).toSource();

0
class FileByLine(object):
  def __init__(self, file_object):
    self.f = file_object

  def next(self):
    # do some line format like <li>line</li>
    data = self.f.readline()
    if data:
      return data
    else:
      raise StopIteration

  def __iter__(self):
    return self

:

return HttpResponse(FileByLine(open('/path/file')))

JS:

$('#lines_from_file').load("<action url>")

http http://ajaxpatterns.org/HTTP_Streaming

0

Source: https://habr.com/ru/post/1749551/


All Articles