0

I am using GAE with django. I am trying to upload a HTML file to the server and the parse it in the server. I Have the parser. All I need is to be able to read it in the code.

Here's my code as of now: The Views:

def upload_file(request):
    from google.appengine.ext import blobstore
    from google.appengine.ext.webapp import blobstore_handlers
    upload_url = blobstore.create_upload_url('/file')
    t = os.path.abspath("templates/upload.html")
    return render(request,t,{'url':upload_url})

def file(request):
    if request.method == 'POST':
          # Get image data
          from app.models.models import Blobs
          b= Blobs()
          avatar = request.FILES['file'].read()
          name = request.POST['name']
          b.html = avatar
          b.name = name
          b.put()
          qry = Blobs.query(Blobs.name == name)
          file = qry.get()
          b.key.delete()
          return HttpResponse(avatar ,content_type="text/html")

my HTML form:

<html>

<body>
    <form action="{{url}}" enctype="multipart/form-data" method="post">
        {% csrf_token %}
        <div>
            <textarea name="name" rows="3" cols="60"></textarea>
        </div>
        <div>
            <label>Avatar:</label>
        </div>
        <div>
            <input type="file" name="file" />
        </div>
        <div>
            <input type="submit" value="upload file">
        </div>
    </form>
    <hr>
    <form>name:
        <input value="%s" name="file_name"> {% csrf_token %}
        <input type="submit" value="switch">
    </form>
</body>

</html>

My Db:

class Blobs(ndb.Model):
    html = ndb.BlobProperty()
    name = ndb.StringProperty(required=True)

And the browser output:

Content-Type: text/html Content-Length: 502302 Content-MD5: OGViN2VhZWIzNTU1YTMzZjFlY2IwNTVjMWEzYjdmNzM= content-disposition: form-data; name="file"; filename="FILE.html" X-AppEngine-Upload-Creation: 2015-09-25 15:49:32.054302

I can't seem to figure out what I'm missing here

Fixed it by using ContentFile function return HttpResponse(ContentFile(avatar),content_type="text/html")

Got my answer in here

Community
  • 1
  • 1
Itamar
  • 1,601
  • 1
  • 10
  • 23
  • 1
    This isn't even close to making sense. You have a field called "img" which you want to have HTML in. You upload something to the blobstore, then for no reason you create a new instance of your model and straight away delete it. What are you trying to do here? – Daniel Roseman Sep 23 '15 at 20:18
  • @DanielRoseman I know the name doesnt make sense. this is just because i have used an existing form. the deleting part is just because i dont want it to upload the file multiple times while testing – Itamar Sep 25 '15 at 15:43
  • What @DanielRoseman said. What are you trying to achieve? Why do you need to upload the HTML file? – MeLight Sep 25 '15 at 16:33
  • @MeLight I need to upload the HTML and then parse is in the server and use the parsed data – Itamar Sep 25 '15 at 18:19
  • @Itamar So which part is not working, the upload, the parsing, or returning the answer? – MeLight Sep 26 '15 at 08:43
  • @MeLight i have problem opening the file in the code and parsing it – Itamar Sep 26 '15 at 09:06
  • Thanks. That same question is linked in the answer :) – Itamar Sep 05 '20 at 11:50

1 Answers1

0

Fixed it by using ContentFile function return HttpResponse(ContentFile(avatar),content_type="text/html")

Found the solution here

Community
  • 1
  • 1
Itamar
  • 1,601
  • 1
  • 10
  • 23