0

I have a requirement to store all static files outside of project folder. though i updated my flask config like below it still not saving images in that folder...

app = Flask(__name__, static_folder='../Storage/static', static_url_path='/storage')

but when i try to save file with below code it always store images inside project_folder->static

def save_image(image: FileStorage, folder: str = None, name: str = None) -> str:
    return IMAGE_SET.save(image, folder, name)

the requirement example:

current

project_folder/
               static/
                      images/

required to store like

Storage(parallel folder)/
               static/
                     images/   

 

Please help, I tried with below links but not helped...

Flask not recognising static folder

How do i link to images not in Static folder in flask

Change static folder from config in Flask

Setting my <img> source to a file path outside of flask static folder

Praveen
  • 346
  • 1
  • 6
  • 18

1 Answers1

0

below are my test code, the folder '../Storage/static/images' should be made before run the code or use os.makedirs() to make first:

import sys,os
from flask import  Flask
from werkzeug.datastructures import FileStorage
file = None
fp=open('110326-666286.jpg', 'rb')
file = FileStorage(fp)

app=Flask(__name__)
app.debug=True

def save_image(image: FileStorage, folder: str = None, name: str = None) -> str:
    if not os.path.exists(folder):
        os.makedirs(folder)
    return image.save(folder+'/'+name)

@app.route('/hello/<name>/<surname>')
def hello(name,surname):
  save_image(file,'../Storage/static/images','110326-666286_.jpg')
  if os.path.exists('../Storage/static/images'+'/'+'110326-666286_.jpg'):
    res='image saved'
  else:
    res='image not saved'
  return '<h1>hello, %s %s</h1><p>%s</p>'%(name,surname,res)



if __name__=='__main__':
  app.run(host='127.0.0.1',port=8080)

below is the result

enter image description here

MLKu
  • 31
  • 3