繁体   English   中英

如何从werkzeug.datastructures将base64编码的字符串转换为可以与FileStorage一起使用的Stream?

[英]How do I convert a base64 encoded string to a Stream that I can use with FileStorage from werkzeug.datastructures?

我需要这个,因为我正在使用AJAX传输文件,因此我得到了以base64编码的字符串形式的图像文件。 我想将其很好地转换为FileStorage对象,以便可以很好地使用flask-uploads库。 知道我该怎么做吗? 谢谢!

看一下pickle模块,特别是pickle.load函数。 该模块有助于将对象与字节流之间来回转换。

您可能还需要使用binascii转换字符串。

def open_file(file_name):
    """
    opens file in samples, and return base64 encoded streams.
        feel free to use this code if it is helpful.
    """
    from backend import backend
    sample_dir = backend.config["samples_dir"]
    file = open(os.path.join(sample_dir, file_name), 'r')
    stream = file.read()
    encoded_stream = base64.b64encode(stream)
    return encoded_stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM