Forums

user uploaded image readable in flask, but not showing in IMG SRC

Hi, I have users uploading files to their own upload folder and my flask app can read these files

@app.route('/files/<fname>')
def page_file_view(fname):
    fullname = os.path.join(as_user.get_user_folder(mod_cfg, get_user()),fname)
    with open(fullname, 'rb') as f:
        txt = f.read()
    return 'viewing file ' + fname + '   first line = ' + str(txt)

shows the files contents

viewing file 4.jpg first line = b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01

When I try and render images, I use a similar method

@app.route('/img_view/<filename>') def send_file(filename): MY_UPLOAD_FOLDER = as_user.get_user_folder(mod_cfg, get_user()) print('MY_UPLOAD_FOLDER = ', MY_UPLOAD_FOLDER) return send_from_directory(MY_UPLOAD_FOLDER, filename)

and to render it I am using in image.html

{% for fname in fullnames %}
<a href="{{  url_for('page_images_view', fname=fname[0] )}}">
  <img src="{{ url_for('send_file', filename=fname[0]) }}"  class="img-fluid" alt="Responsive image">
</div>
</a>
{% endfor %}

The UPLOAD_FOLDER is USER_DATA/duncan/UPLOADS and the error message I get in the image view is

2018-04-18 03:08:50,497: Exception on /img_view/4.jpg [GET]#012Traceback (most recent call last):#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app#012 response = self.full_dispatch_request()#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request#012 rv = self.handle_user_exception(e)#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception#012 reraise(exc_type, exc_value, tb)#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise#012 raise value#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request#012 rv = self.dispatch_request()#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request#012 return self.view_functionsrule.endpoint#012 File "/home/acutesoftware/netdiary/net_diary.py", line 1196, in send_file#012 return send_from_directory(MY_UPLOAD_FOLDER, filename)#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/helpers.py", line 616, in send_from_directory#012 return send_file(filename, **options)#012 File "/home/acutesoftware/.virtualenvs/myvirtualenv/lib/python3.4/site-packages/flask/helpers.py", line 520, in send_file#012 file = open(filename, 'rb')#012FileNotFoundError: [Errno 2] No such file or directory: '/home/acutesoftware/netdiary/USER_DATA/duncan/UPLOADS/4.jpg'

Any ideas why I can read the file, but it is not showing?

Thanks

Is the file at /home/acutesoftware/netdiary/USER_DATA/duncan/UPLOADS/4.jpg? If not, where is it? And you probably want to have a look at this http://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/

Well, the file is actually at /home/acutesoftware/USER_DATA/duncan/UPLOADS

but the image page is looking for

/home/acutesoftware/netdiary/USER_DATA/duncan/UPLOADS/4.jpg'

Thanks for your help - yes it is fixed now (turns out I was sending different paths to file and image, in that send_file requires the path but the file view took the full filename.

So, yes - all fixed!

Cheers

Excellent, thanks for confirming!