使用 POST 在网页中包含动态图像?

发布于 2024-07-07 20:10:53 字数 322 浏览 9 评论 0 原文

我编写了一个 CGI 脚本,它使用 GET 数据动态创建图像。 为了在我的网页中包含此图像,我使用以下代码:

<img src="image.py?text=xxxxxxxxxxxxxx">

问题是我预计将来“文本”字段会变得很长并且 URL 会变得太大。 从谷歌搜索来看,URL 长度似乎没有固定的限制(即取决于浏览器、服务器、代理等)是否有更好的方法来做到这一点?

如果重要的话,我正在使用 Django 和 Python,并且无法使用任何客户端脚本(即 JavaScript)。

干杯, 本

I have written a CGI script that creates an image dynamically using GET data. To include this image in my webpage, I am using the following code:

<img src="image.py?text=xxxxxxxxxxxxxx">

The problem is that I expect in the future the "text" field will get very long and the URL will become too large. From Googling around there doesn't seem to be a fixed limit on URL length (ie. depends on the browser, server, proxy, etc.) Is there a better way to do this?

If it matters, I am working with Django and Python and I cannot use any client-side scripting (ie. JavaScript).

Cheers,
Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

森林迷了鹿 2024-07-14 20:10:53

将文本存储在某个地方(例如数据库),然后传递主键。

Store the text somewhere (e.g. a database) and then pass through the primary key.

浅浅 2024-07-14 20:10:53

这将为您提供一个作为 POST 结果的图像 - 您可能不喜欢它

  1. 在您想要图像的位置放置一个 iFrame 并调整其大小并删除滚动条
  2. 将 src 设置为表单,并将隐藏输入设置为您的帖子参数和操作设置为将生成图像的 URL
  3. 在 iFrame 的 HTML 的 body.onload 中使用 JavaScript 自动提交表单

    然后,要么:

  4. 返回设置为图像的内容类型并流式传输图像字节

    或:

  5. 将post参数存储在某处并生成一个小id

  6. 使用 url 中的 id 提供带有 img 标签的 HTML -- 在服务器上查找 post 参数

    或者:

  7. 生成一个带有嵌入图像的图像标签的页面

    http://danielmclaren.net/2008 /03/embedding-base64-image-data-into-a-webpage

This will get you an Image as the result of a POST -- you may not like it

  1. Put an iFrame where you want the image and size it and remove scrollbars
  2. Set the src to a form with hidden inputs set to your post parameters and the action set to the URL that will generate the image
  3. submit the form automatically with JavaScript in the body.onload of the iFrame's HTML

    Then, either:

  4. Serve back an content-type set to an image and stream the image bytes

    or:

  5. store the post parameters somewhere and generate a small id

  6. serve back HTML with an img tag using the id in the url -- on the server look up the post parameters

    or:

  7. generate a page with an image tag with an embedded image

    http://danielmclaren.net/2008/03/embedding-base64-image-data-into-a-webpage

椒妓 2024-07-14 20:10:53

将已经说过的内容放在一起,创建两个页面怎么样? 当表单提交时(比如 create_img.py),第一页会发送一个带有 text=xxxxxxx... 参数的 POST 请求。 然后 create_img.py 获取文本参数并用它创建一个图像并将其(或文件系统引用)插入到数据库中,然后在渲染第二个页面时,生成 img 标签,例如 。 此时,render_img.py 只是在数据库中查询给定的图像。 在创建图像之前,您可以检查它是否已在数据库中,因此可以使用相同的文本参数重用/回收以前的图像。

Putting together what has already been said, how about creating two pages. First page sends a POST request when the form is submitted (lets say to create_img.py) with a text=xxxxxxx... parameter. Then create_img.py takes the text parameter and creates an image with it and inserts it (or a filesystem reference) into the db, then when rendering the second page, generate img tags like <img src="render_img.py?row_id=0122">. At this point, render_img.py simply queries the db for the given image. Before creating the image you can check to see if its already in the database therefore reusing/recycling previous images with the same text parameter.

日暮斜阳 2024-07-14 20:10:53

img 使用 GET。 你必须想出另一种机制。 如何在 image.py 中调用相同的功能并将文件保存为您在 img 标记中引用的临时文件? 或者如何在渲染此 img 标记期间将文本值保存在数据库行中,并使用 row_id 作为传递到 image.py 脚本中的内容?

img's use GET. You'll have to come up with another mechanism. How about calling the same functionality in image.py and saving the file as a temp file which you ref in the img tag? Or how about saving the value of text in a db row during the rendering of this img tag and using the row_id as what you pass into the image.py script?

毁虫ゝ 2024-07-14 20:10:53

您可以通过压缩 get 参数中的文本来缓解该问题。

You may be able to mitigate the problem by compressing the text in the get parameter.

感情废物 2024-07-14 20:10:53

从下面的链接看来,您暂时会没事;)

http:// /www.boutell.com/newfaq/misc/urllength.html

From the link below it looks like you'll be fine for a while ;)

http://www.boutell.com/newfaq/misc/urllength.html

沙与沫 2024-07-14 20:10:53

如果您使用 django,也许您可​​以通过模板标签来完成此操作?

类似于:

<img src="{% create_image "This is the text that will be displayed" %}">

create_image 函数将使用虚拟/随机/生成的文件名创建图像,并返回路径。

这避免了对脚本进行 GET 或 POST 操作,并且图像将具有可管理的文件名。

我可以看到这种方法的一些潜在问题,我只是把这个想法抛在那里;)

If you're using django, maybe you can do this via a template tag instead?

Something like:

<img src="{% create_image "This is the text that will be displayed" %}">

The create_image function would create the image with a dummy/random/generated filename, and return the path.

This avoids having to GET or POST to the script, and the images will have manageable filenames.

I can see some potential issues with this approach, I'm just tossing the idea out there ;)

流绪微梦 2024-07-14 20:10:53

好吧,我有点晚了,但是您可以混合使用 MHTML(适用于 IE7 及更低版本)和数据 URI 方案(适用于所有其他现代浏览器)。 它确实需要在客户端和服务器上进行一些工作,但您最终可以得到有关

newimg.src = 'blah';

如何执行此操作的文章位于 http://gingerbbm.com/?p=127

OK, I'm a bit late to the party, but you could use a mix of MHTML (for IE7 and below) and the data URI scheme (for all other modern browsers). It does require a bit of work on both client and server but you can ultimately end up with

newimg.src = 'blah';

The write-up on how to do this is at http://gingerbbm.com/?p=127.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文