gpt4 book ai didi

python - 在 Python、ASP.NET 中读取和返回 jpg 文件?

转载 作者:太空狗 更新时间:2023-10-30 00:18:32 28 4
gpt4 key购买 nike

我有一个使用 Python 的 image.asp 文件,我试图在其中打开一个 JPEG 图像并将其写入响应,以便可以从相关链接中检索它。我目前拥有的:

<%@ LANGUAGE = Python%>
<%

path = "path/to/image.jpg"

with open(path, 'rb') as f:
jpg = f.read()

Response.ContentType = "image/jpeg"

Response.WriteBinary(jpg)

%>

在浏览器中,这将返回以下错误:

The image "url/to/image.asp" cannot be displayed because it contains errors.

我怀疑问题是我没有正确写入 jpg 文件的内容。我需要修复什么?

最佳答案

您的问题在这里:

 with open(url, 'rb') as f:

包含路径的变量被命名为路径,而不是 url。

让它成为:

 with open(path, 'rb') as f:

而且效果会更好。

关于python - 在 Python、ASP.NET 中读取和返回 jpg 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51165448/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com