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