I want to upload an image file to imgur
using the python-Flask. So for this I am submitting an image file from the form and want to upload this file to imgur.
But the point is the example snippets given in the imgur api want the path name of the file that was uploaded. So far I was trying to upload but I am stuck!!
This is my main.py
file
if request.method == "POST":
image = request.files.get('myfile') #myfile is name of input tag
config ={
'album':album,
'name':'Catastrophe!',
'title':'Catastrophe!'
}
print os.path.realpath(image.filename) # this line gives me wrong path of file.
print "uploading image..."
#image = client.upload_from_path(filepath,config=config,anon=False)
The commented print statement gives me path like this
/home/suraj/Desktop/FlaskTrials/wallpaper.jpg
But the thing is the correct file path could be anything the user wants to choose image from
How do I get this path. Am I doing the right thing to upload the image to imgur api ?
I would be able to do by making an dir in root folder and adding image to it and then get the filename of that and upload to imgur.
But I was wandering is it possible without saving and image file.
Thanks in advance
It looks to me like you forgot to save the file on the server. Here is a modified version of your code based on http://flask.pocoo.org/docs/0.10/patterns/fileuploads/.
if request.method == "POST":
image = request.files['myfile'] #myfile is name of input tag
config ={
'album':album,
'name':'Catastrophe!',
'title':'Catastrophe!'
}
print "uploading image..."
filename = secure_filename(image.filename)
file.save(os.path.join('/home/suraj/Pictures', filename))
print os.path.realpath(image.filename)
I recommend considering restricting the file names to certain extensions, as suggested by the Flask doc.