使用烧瓶中的图像路径上传图像

upload image using image Path in flask

我想使用 python-Flask 将图像文件上传到 imgur。因此,为此我从表单中提交了一个图像文件,并希望将该文件上传到 imgur。

但重点是 imgur api 中给出的示例片段需要上传文件的路径名。到目前为止,我一直在尝试上传,但卡住了!!

这是我的 main.py 文件

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)

注释的打印语句给了我这样的路径

/home/suraj/Desktop/FlaskTrials/wallpaper.jpg

但问题是正确的文件路径可以是用户想要从中选择图像的任何内容

如何获得这条路径。我将图片上传到 imgur api 是否正确?

我可以通过在根文件夹中创建一个目录并向其中添加图像,然后获取该文件名并上传到 imgur 来实现。

但我在徘徊是否可以不保存和图像文件。

提前致谢

我看你好像忘记在服务器上保存文件了。这是基于 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)

我建议按照 Flask 文档的建议,考虑将文件名限制为某些扩展名。