1

Basically I'm working in php laravel.

I've just started to work with Flask. I'm developing API in Flask. I'm trying to upload image file from Api to the Ubuntu server. Also I wanted to share image url.

Following code is working locally but not able to retrieve image from URL. Same code is working in ubuntu but image is not getting stored in said location. I'm thinking I'm not giving proper path.

    file = request.files['file']
    file_name = str(current_user.id) +".jpg"
    file.save(os.path.join("static/corporate_employee_images/", file_name))

I tried to store image content in database and retrieve but that too isn't working.getting read_image not found error.

I just wanted to upload image and return its url. In above case

http://127.0.0.1:5000/static/corporate_employee_images/10.jpg

How can I achieve this.

Please guide.

Thanks

6
  • What have you tried so far? Commented Dec 12, 2018 at 7:33
  • I actually tried all possible way which I have I'll share code after finishing food.code working locally but not on Ubuntu. Commented Dec 12, 2018 at 7:35
  • And How should people trying to answer your question know what you tried? Commented Dec 12, 2018 at 7:37
  • Sorry. I'll share my code and my findings Commented Dec 12, 2018 at 7:38
  • That would be very helpful Commented Dec 12, 2018 at 7:42

1 Answer 1

1

So right now you're saving the file in the current working directory, which does not have to be the same as the app folder.

So you need to save the file in the right folder, you also need to take into account the location of your flask app.

So change:

file.save(os.path.join("static/corporate_employee_images/", file_name))

to:

file.save(os.path.join(app.root_path, "static/corporate_employee_images/", file_name))
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks. And how can I access it? I meant URL
url_for('static', filename="static/corporate_employee_images/" + file_name)
This is for html or for api? I mean I'm returning response in json. This will work? { "data": [ { "activities": 3, "email_id": "", "entity_name": "", "hours": 5, "hours_percentag": 15, "image": "127.0.0.1:5000/static/corporate_employee_images/10.jpg", "interest": "Mumbai", "job_information": "hero", "level": "novice", "location": "Mumbai", "organizations": 2, } ], "status": "success" }
It shouldn't matter since it's a relative path anyways. It just returns static/corporate_employee_images/10.jpg. You can then add the start of the url in js.
Its purely Api, No Html,js used.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.