How do I send a binary blob to a client browser?

有些话、适合烂在心里 提交于 2019-12-10 11:57:56

问题


Pardon the dumb newbie question here; web programming isn't my forte... (blush)

I have an aspx page running on a web server. I have a blob (byte array) containing any kind of binary file, plus a file name.

I would like to push this file to be downloaded through the browser onto the client, and opened using whatever application is default for this file type. I really don't want to save the blob as a file on the server; that will leave a terrible housekeeping mess that I just don't want to think about.

I did try googling this question, but I guess I'm using the wrong keywords.

This really should be obvious how to do it, but I'm having no joy.

What is the trick?

Thanks!


回答1:


Response.BinaryWrite(byteArray);

You should also set the content type

Response.ContentType = "application/pdf";

But that will be based on your file type.

And the file name (and everything together) is done like this

Response.AddHeader("content-disposition", 
   String.Format("attachment;filename={0}", fileName));    
Response.ContentType = "application/pdf";
Response.BinaryWrite(byteArray);



回答2:


First, you have to know the mime type. Once you know that, you can set the Response.ContentType property. After that, just use Response.BinaryWrite(). If you don't first set the ContentType property, the client will have almost no chance of opening the file correctly.



来源:https://stackoverflow.com/questions/426219/how-do-i-send-a-binary-blob-to-a-client-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!