开发者

Render image or pdf stream from SQL database in asp.net

I have a table with documents saved some of them in pdf, some of them image. I want to create a web app, to show the images (that can be either pdf, either jpg) in the same control.

I can manage to see pdf, if I set the Response.ContentType = "application/pdf" or image if I set "application/jpg". But the problem is that how can I get the f开发者_开发知识库ile type, having only the stream saved into the database? Does it have the stream the file type information in it?

Thanks.


No, a stream does not have a content type associated with it. If you had the original filename, you could attempt to derive the content type from that, but it wouldn't be foolproof.

Many file formats have a series of "magic bytes" that allow you to detect what (might) be in the file. PDF, for example, begins with the bytes "%PDF" (note: I'm not an expert on PDF, and there may be situations where that is not true).

If you have no other option, you could attempt to parse the file using various libraries until you found one that worked (System.Drawing.Image.FromStream(), iTextSharp, etc).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜