禁止直接访问图像
我正在制作一个小家庭相册,打算稍后向其他人开放以存储图像。
我将图像上传到 ~\images\,然后调整它们大小 3 次(普通视图...缩略图和小版本)并将它们移动到 ~\images\thumbs、~\images\normal、~\images\tiny,然后将原件移动到 ~\images\original。
如果用户知道文件名,他们只需转到 http://mysite.com/images/normal/filename.jpg< /a> 用于直接访问。
我不希望他们可以使用它。
那么,有没有办法更改 asp:Image 控件的 ImageUrl,而不是从不可访问的文件夹中读取?这会影响性能吗?我在想(不确定是否可能)将图像读入 Steam,并以某种方式设置 ImageUrl 或其他什么,以从流中读取?
希望您能帮忙。
I am making a little family photo album, with the intention to maybe open it to other people to store images later.
I upload the images to ~\images\, then resize them 3 times (Normal view ... thumbnail and a tiny version) and move them to ~\images\thumbs, ~\images\normal, ~\images\tiny and then move the original to ~\images\original.
If a user knows a file name, they can just goto http://mysite.com/images/normal/filename.jpg for direct access.
I'd prefer that not to be available to them.
So, is there a way to change the ImageUrl of the asp:Image control, to rather read from a non-accessable folder? And would this be a performance hit? I'm thinking something like (Not sure if it's possible) reading the image into s Steam, and somehow setting the ImageUrl or what ever, to read from the stream?
Hope you can assist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案是肯定的。您可以使用特殊格式的 img src ,它将图像直接嵌入到页面中。
您可以使用以下格式而不是 URL:
其中 data 指定图像的 mime 类型,base64 之后的 ... 是图像的 base64 编码内容。
此行为由此 RFC 定义。
下面是一个示例:
在 HTML 中:
在隐藏代码中:
The answer is yes. You can use a special format of img src which will embed the image directly in the page.
Instead of the URL, you would use the following format:
where data specifies the mime type of your image and the ... after base64, is the base64 encoded contents of the image.
This behavior is defined by this RFC.
Here is an example:
In the HTML:
In the code-behind:
我可以建议考虑类似的事情:
Session["user"] = youruser;
无论您在会话变量中存储什么对象,都包含有效的 youruser从数据库中提取的 .userID。未经测试的代码)
这应该有效地拒绝除各自“图像”文件夹的正确用户之外的所有人的访问,即使他们找出图像文件的直接路径。
如果有人有任何方法来改进这种方法,请随时发表评论,因为我很快就会在网站上实现这个想法,并且随时欢迎第二意见。
Might I suggest considering something like:
Session["user"] = youruser;
Whatever object you store in your session variable include a valid youruser.userID that is pulled from a database.(untested code)
this should effectively deny access to everyone but the correct user of their respective "images" folder, even if they figure out the direct path of the image files.
If anyone has any ways to improve this method feel free to comment as I will soon be implementing this idea in a site and second opinions are always welcome.