我编写了一个 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
发布评论
评论(8)
将文本存储在某个地方(例如数据库),然后传递主键。
Store the text somewhere (e.g. a database) and then pass through the primary key.
这将为您提供一个作为 POST 结果的图像 - 您可能不喜欢它
在 iFrame 的 HTML 的 body.onload 中使用 JavaScript 自动提交表单
然后,要么:
返回设置为图像的内容类型并流式传输图像字节
或:
将post参数存储在某处并生成一个小id
使用 url 中的 id 提供带有 img 标签的 HTML -- 在服务器上查找 post 参数
或者:
生成一个带有嵌入图像的图像标签的页面
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
submit the form automatically with JavaScript in the body.onload of the iFrame's HTML
Then, either:
Serve back an content-type set to an image and stream the image bytes
or:
store the post parameters somewhere and generate a small id
serve back HTML with an img tag using the id in the url -- on the server look up the post parameters
or:
generate a page with an image tag with an embedded image
http://danielmclaren.net/2008/03/embedding-base64-image-data-into-a-webpage
将已经说过的内容放在一起,创建两个页面怎么样? 当表单提交时(比如 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.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?
您可以通过压缩 get 参数中的文本来缓解该问题。
You may be able to mitigate the problem by compressing the text in the get parameter.
从下面的链接看来,您暂时会没事;)
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
如果您使用 django,也许您可以通过模板标签来完成此操作?
类似于:
create_image 函数将使用虚拟/随机/生成的文件名创建图像,并返回路径。
这避免了对脚本进行 GET 或 POST 操作,并且图像将具有可管理的文件名。
我可以看到这种方法的一些潜在问题,我只是把这个想法抛在那里;)
If you're using django, maybe you can do this via a template tag instead?
Something like:
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 ;)
好吧,我有点晚了,但是您可以混合使用 MHTML(适用于 IE7 及更低版本)和数据 URI 方案(适用于所有其他现代浏览器)。 它确实需要在客户端和服务器上进行一些工作,但您最终可以得到有关
如何执行此操作的文章位于 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
The write-up on how to do this is at http://gingerbbm.com/?p=127.