Uploading an image to Flask server

2024/11/16 6:36:51

I am struggling bit with Flask and uploading a file, here is my Flask code so far:

    @app.route('/api/user/update/', methods=['PUT'])
@auth.login_required
def update_user():# check if the post request has the file partuser = User.query.filter_by(id=g.user.id,deleted=0).first()try:if 'uname' in request.args:user.username = request.args['uname']if 'password' in request.args:user.hash_password(request.args['password'])if 'lname' in request.args:user.lastname = request.args['lname']if 'fname' in request.args:user.firstname = request.args['fname']if 'address' in request.args:user.address = request.args['address']if 'preferance1' in request.args:user.pre1 = request.args['preferance1']if 'preferance2' in request.args:user.pre2 = request.args['preferance2']if 'preferance3' in request.args:user.pre3 = request.args['preferance3']if 'file' in request.files:file = request.files['file']filename = secure_filename(file.filename)filename.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))#print(filename)#print(url_for('upload_file', filename=filename))user.image_filename = filenameuser.image_url = url_for('upload_file', filename=filename)db.session.commit()except:print("Error")db.session.rollback()db.session.flush()return jsonify({'user':user.serialize})

If I added anything as a file from Postman the request will failed and exception will be thrown. I am not sure why.

Answer

I think you should write file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) instead of filename.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)). The file is a File Storage object. And the filename is only a str.

https://en.xdnf.cn/q/119562.html

Related Q&A

Enemy Projectiles Arent Appending On Screen

I have here my script that targets the player what ever position he is at but the projectiles arent showing on my screen VIDEO. He isnt attacking at all, I dont know why. I am in my main loop I draw t…

Python+Selenium. Cant locate element

Ive implemented the script using Python and selenium to click on the ads. But now this script is not working.Unable to find element on the page.Please help me to correct the script. Thank you!from sele…

AttributeError: NoneType object has no attribute channels [duplicate]

This question already has answers here:Why do I get AttributeError: NoneType object has no attribute something?(11 answers)Closed 5 years ago.Hi Im having an issue with a module for my Discord bot. Im…

How to get a specific value from a html header

Im using selenium to get request headers from a web page, the problem is that it prints out all request headers sent and i want to get only one value from one of them. I dont know how to do it and i ha…

How to use windows as raspberry pi and connect the windows with another raspberry pi [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Python : Return the missing weekdays dates and assign rate next to missing date

Dates rates 7/26/2019 1.04 7/30/2019 1.0116 7/31/2019 1.005 8/1/2019 1.035 8/2/2019 1.01 8/6/2019 0.9886 8/7/2019 1.0048 8/8/2019 0.97 8/9/2019 0.9659 8/12/2019 0.965In …

How do I use a list or set as keys in file renaming

Is something like this possible? Id like to use a dictionary or set as the key for my file renamer. I have a lot of key words that id like to filter out of the file names but the only way iv found to …

How to change Python comment font style in the latest VS Code? [duplicate]

This question already has an answer here:How to change the font-style of code comments in vscode?(1 answer)Closed 6 months ago.Seems like with the latest VS Code update, all the comment font style has…

How to crop an image based on a complex criteria?

I have a set of similar images like the one below. I want to keep the portion of the image that is within the top red irregular rectangle (green arrows represent the space that I want to keep; anything…

Getting current video tag URL with selenium

Im trying to get the current html5 video tag URL using selenium (with python bindings):from selenium import webdriverdriver = webdriver.Chrome() driver.get(https://www.youtube.com/watch?v=9x6YclsLHN0)…