简体   繁体   English

如何从具有ASPX扩展名的URL提供图像?

[英]How is an image served from a URL with an ASPX extension?

Can Somebody tell how to create such kind of urls 有人可以告诉如何创建这样的网址

for example if you see the url 例如,如果你看到网址

http://office.microsoft.com/global/images/default.aspx?assetid=ZA103873861033 http://office.microsoft.com/global/images/default.aspx?assetid=ZA103873861033

you will redirect to an image .. 你将重定向到一个图像..

my question is , though this url is an image..its extension is aspx..how is it possible. 我的问题是,虽然这个url是一个图像..扩展名是aspx ..它是否可能。 how to create such kind of url's 如何创建这种网址

Thanks 谢谢

通过在服务器的响应中设置ContentType

  HttpContext.Response.ContentType = "image/jpeg";

This is a common method for displaying an image that's stored as a binary object in a database. 这是用于显示在数据库中存储为二进制对象的图像的常用方法。 One tutorial, among many, can be found here . 其中一个教程可以在这里找到。

Essentially, what they're doing is using the aspx page to accept the URL parameter which tells them what image to fetch from the database. 基本上,他们正在做的是使用aspx页面接受URL参数,该参数告诉他们从数据库中获取什么图像。 Then in the response they clear all output and headers, set the headers for the image, write the binary data to the response stream, and close the response stream. 然后在响应中,它们清除所有输出和标题,设置图像的标题,将二进制数据写入响应流,并关闭响应流。

So it's not really "redirecting" you to an image. 所以它并没有真正“重定向”你的形象。 The "page" being requested turns out to be an image resource in the response. 被请求的“页面”原来是响应中的图像资源。

easiest way is to add generic handler *.ashx and in ashx file u'll have code behind which u can get querystring and manipulate response eg. 最简单的方法是添加通用处理程序* .ashx并在ashx文件中你将拥有代码,你可以在其后获得查询字符串并操纵响应,例如。 Response.WriteFile(...) Response.WriteFile(...)

File extensions literally have no meaning on the WWW. 文件扩展名字在WWW上没有任何意义。 The thing that correctly describes the content at a particular URL is the content-type/ MIME-type . 正确描述特定URL上的内容的是内容类型/ MIME类型 This is delivered in an HTTP header when the URL is requested prior to delivery of the main HTTP payload. 当在传递主HTTP有效负载之前请求URL时,这将在HTTP头中提供。 Other answers describe how you might correctly set this in ASP.NET. 其他答案描述了如何在ASP.NET中正确设置它。

Aside from all other answers they may be doing a Server.Transfer() (so that you don't see it client-side) to the image file. 除了所有其他答案,他们可能正在对图像文件执行Server.Transfer() (以便您不会在客户端看到它)。 This still means the response headers are being set to the appropriate MIME type but it also means the image isn't necesarilly coming from a database. 这仍然意味着响应标头被设置为适当的MIME类型,但它也意味着图像不是来自数据库的necesarilly。 This technique can be used to hide the actual image URL in attempts to prevent hotlinking. 此技术可用于隐藏实际图像URL以尝试防止热链接。

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

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