librelist archives

« back to archive

how can I return a file without using static folder?

how can I return a file without using static folder?

From:
Fazheng Li
Date:
2013-03-02 @ 13:32
I am now writing a blog under GAE.

In my blog ,I need to store some  images on GAE using "blob" .
for example , when I upload a image to the server, I use the following code
to store the images:

image_data=db.Blob(request,form['images'].read())

and the "image_data" are stored in the database, instead of any folder on
file system  .
so how can I return the image when somebody request like GET
/images/img.jpg ?

Re: [flask] how can I return a file without using static folder?

From:
Markus Unterwaditzer
Date:
2013-03-02 @ 13:47
Fazheng Li <fzleee@gmail.com> wrote:

> I am now writing a blog under GAE.
> 
> In my blog ,I need to store some  images on GAE using "blob" .
> for example , when I upload a image to the server, I use the following
> code
> to store the images:
> 
> image_data=db.Blob(request,form['images'].read())
> 
> and the "image_data" are stored in the database, instead of any folder
> on
> file system  .
> so how can I return the image when somebody request like GET
> /images/img.jpg ?

Use flask.send_file.

http://flask.pocoo.org/docs/api/#flask.send_file

Re: [flask] how can I return a file without using static folder?

From:
Alex Ogier
Date:
2013-03-02 @ 16:35
On Sat, Mar 2, 2013 at 8:47 AM, Markus Unterwaditzer <
markus@unterwaditzer.net> wrote:

> Fazheng Li <fzleee@gmail.com> wrote:
>
> > I am now writing a blog under GAE.
> >
> > In my blog ,I need to store some  images on GAE using "blob" .
> > for example , when I upload a image to the server, I use the following
> > code
> > to store the images:
> >
> > image_data=db.Blob(request,form['images'].read())
> >
> > and the "image_data" are stored in the database, instead of any folder
> > on
> > file system  .
> > so how can I return the image when somebody request like GET
> > /images/img.jpg ?
>
> Use flask.send_file.
>
> http://flask.pocoo.org/docs/api/#flask.send_file
>

send_file is actually for sending things from the filesystem. What you want
to do is create a Response object with a mimetype argument that is
"image/jpg," "image/png" or whatever format of image you are returning and
the body of the response should be the raw data. For example:

from flask import Response
@app.route('/image')
def image():
    # load image_data from database
    return Response(image_data, mimetype='image/jpg') # or image/gif or
image/png

That should send the image that is stored in the database.

Best,
Alex Ogier

Re: [flask] how can I return a file without using static folder?

From:
Steven Kryskalla
Date:
2013-03-02 @ 17:02
On Sat, Mar 2, 2013 at 8:35 AM, Alex Ogier <alex.ogier@gmail.com> wrote:
> send_file is actually for sending things from the filesystem.

It works for more than just files from the filesystem. The first
argument to send_file is "filename_or_fp", and the docs say "a file
object [can] be provided". That means you could use StringIO to create
a file object in memory whose contents came from the database (or
wherever).

Re: [flask] how can I return a file without using static folder?

From:
Fazheng Li
Date:
2013-03-03 @ 01:50
Yes ,I think both of the two methods can help me to solve the problem.
I have tried both of th two methods ,but I got the same problem. :

Traceback (most recent call last):
File "/home/fzlee/Tool/google_appengine/google/appengine/runtime/wsgi.py",
line 224, in Handle
for chunk in result:
File "/home/fzlee/WWW/mypress/flaskext/gae_mini_profiler/profiler.py", line
303, in __call__
result = self.app(environ, start_response)
File "/home/fzlee/WWW/mypress/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/fzlee/WWW/mypress/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/home/fzlee/WWW/mypress/flask/app.py", line 1362, in
full_dispatch_request
response = self.process_response(response)
File "/home/fzlee/WWW/mypress/flask/app.py", line 1564, in process_response
response = handler(response)
File "/home/fzlee/WWW/mypress/flaskext/gae_mini_profiler/__init__.py", line
115, in _process_response
response_html = response.data.decode(response.charset)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
*UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
invalid start byte*

it seems that flask is using utf8 to decode the images,so how can I get
through this? thank you





2013/3/3 Steven Kryskalla <skryskalla@gmail.com>

> On Sat, Mar 2, 2013 at 8:35 AM, Alex Ogier <alex.ogier@gmail.com> wrote:
> > send_file is actually for sending things from the filesystem.
>
> It works for more than just files from the filesystem. The first
> argument to send_file is "filename_or_fp", and the docs say "a file
> object [can] be provided". That means you could use StringIO to create
> a file object in memory whose contents came from the database (or
> wherever).
>

Re: [flask] how can I return a file without using static folder?

From:
Alex Ogier
Date:
2013-03-03 @ 02:25
Hi Fazheng,

It looks like Flask-GAE-Mini-Profiler is trying to add its HTML bits to
your image, but it shouldn't since the image doesn't have the right content
type. This looks like a known issue:
https://github.com/passy/flask-gae-mini-profiler/issues/6

For now you can try the workaround in that issue:
https://github.com/passy/flask-gae-mini-profiler/issues/6#issuecomment-4814356

I think in the long run, Flask-GAE-Mini-Profiler should start checking
response.mimetype and only instrumenting HTML, but for now that should work.

Best,
Alex Ogier


On Sat, Mar 2, 2013 at 8:50 PM, Fazheng Li <fzleee@gmail.com> wrote:

> Yes ,I think both of the two methods can help me to solve the problem.
> I have tried both of th two methods ,but I got the same problem. :
>
> Traceback (most recent call last):
> File "/home/fzlee/Tool/google_appengine/google/appengine/runtime/wsgi.py",
> line 224, in Handle
> for chunk in result:
> File "/home/fzlee/WWW/mypress/flaskext/gae_mini_profiler/profiler.py",
> line 303, in __call__
> result = self.app(environ, start_response)
> File "/home/fzlee/WWW/mypress/flask/app.py", line 1689, in wsgi_app
> response = self.make_response(self.handle_exception(e))
> File "/home/fzlee/WWW/mypress/flask/app.py", line 1687, in wsgi_app
> response = self.full_dispatch_request()
> File "/home/fzlee/WWW/mypress/flask/app.py", line 1362, in
> full_dispatch_request
> response = self.process_response(response)
> File "/home/fzlee/WWW/mypress/flask/app.py", line 1564, in process_response
> response = handler(response)
> File "/home/fzlee/WWW/mypress/flaskext/gae_mini_profiler/__init__.py",
> line 115, in _process_response
> response_html = response.data.decode(response.charset)
> File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
> return codecs.utf_8_decode(input, errors, True)
> *UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
> invalid start byte*
>
> it seems that flask is using utf8 to decode the images,so how can I get
> through this? thank you
>
>
>
>
>
> 2013/3/3 Steven Kryskalla <skryskalla@gmail.com>
>
>> On Sat, Mar 2, 2013 at 8:35 AM, Alex Ogier <alex.ogier@gmail.com> wrote:
>> > send_file is actually for sending things from the filesystem.
>>
>> It works for more than just files from the filesystem. The first
>> argument to send_file is "filename_or_fp", and the docs say "a file
>> object [can] be provided". That means you could use StringIO to create
>> a file object in memory whose contents came from the database (or
>> wherever).
>>
>
>

Re: [flask] how can I return a file without using static folder?

From:
Fazheng Li
Date:
2013-03-03 @ 02:32
Yes,that exactly helps! thank you!

2013/3/3 Alex Ogier <alex.ogier@gmail.com>

> Hi Fazheng,
>
> It looks like Flask-GAE-Mini-Profiler is trying to add its HTML bits to
> your image, but it shouldn't since the image doesn't have the right content
> type. This looks like a known issue:
> https://github.com/passy/flask-gae-mini-profiler/issues/6
>
> For now you can try the workaround in that issue:
> https://github.com/passy/flask-gae-mini-profiler/issues/6#issuecomment-4814356
>
> I think in the long run, Flask-GAE-Mini-Profiler should start checking
> response.mimetype and only instrumenting HTML, but for now that should work.
>
> Best,
> Alex Ogier
>
>
> On Sat, Mar 2, 2013 at 8:50 PM, Fazheng Li <fzleee@gmail.com> wrote:
>
>> Yes ,I think both of the two methods can help me to solve the problem.
>> I have tried both of th two methods ,but I got the same problem. :
>>
>> Traceback (most recent call last):
>> File
>> "/home/fzlee/Tool/google_appengine/google/appengine/runtime/wsgi.py", line
>> 224, in Handle
>> for chunk in result:
>> File "/home/fzlee/WWW/mypress/flaskext/gae_mini_profiler/profiler.py",
>> line 303, in __call__
>> result = self.app(environ, start_response)
>> File "/home/fzlee/WWW/mypress/flask/app.py", line 1689, in wsgi_app
>> response = self.make_response(self.handle_exception(e))
>> File "/home/fzlee/WWW/mypress/flask/app.py", line 1687, in wsgi_app
>> response = self.full_dispatch_request()
>> File "/home/fzlee/WWW/mypress/flask/app.py", line 1362, in
>> full_dispatch_request
>> response = self.process_response(response)
>> File "/home/fzlee/WWW/mypress/flask/app.py", line 1564, in
>> process_response
>> response = handler(response)
>> File "/home/fzlee/WWW/mypress/flaskext/gae_mini_profiler/__init__.py",
>> line 115, in _process_response
>> response_html = response.data.decode(response.charset)
>> File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
>> return codecs.utf_8_decode(input, errors, True)
>> *UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
>> invalid start byte*
>>
>> it seems that flask is using utf8 to decode the images,so how can I get
>> through this? thank you
>>
>>
>>
>>
>>
>> 2013/3/3 Steven Kryskalla <skryskalla@gmail.com>
>>
>>> On Sat, Mar 2, 2013 at 8:35 AM, Alex Ogier <alex.ogier@gmail.com> wrote:
>>> > send_file is actually for sending things from the filesystem.
>>>
>>> It works for more than just files from the filesystem. The first
>>> argument to send_file is "filename_or_fp", and the docs say "a file
>>> object [can] be provided". That means you could use StringIO to create
>>> a file object in memory whose contents came from the database (or
>>> wherever).
>>>
>>
>>
>