如何将二进制 blob 发送到客户端浏览器?

发布于 2024-07-10 17:01:20 字数 316 浏览 1 评论 0原文

请原谅这里愚蠢的新手问题; 网络编程不是我的强项...(脸红)

我有一个在网络服务器上运行的 aspx 页面。 我有一个包含任何类型的二进制文件以及文件名的 blob(字节数组)。

我想将此文件通过浏览器下载到客户端,并使用此文件类型默认的任何应用程序打开。 我真的不想将 blob 保存为服务器上的文件; 这会留下可怕的家务混乱,我只是不想考虑。

我确实尝试用谷歌搜索这个问题,但我想我使用了错误的关键字。

这确实应该是显而易见的如何做到这一点,但我没有喜悦。

有什么窍门呢?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-07-17 17:01:20
Response.BinaryWrite(byteArray);

您还应该设置内容类型,

Response.ContentType = "application/pdf";

但这将基于您的文件类型。

文件名(以及所有内容)是这样完成的

Response.AddHeader("content-disposition", 
   String.Format("attachment;filename={0}", fileName));    
Response.ContentType = "application/pdf";
Response.BinaryWrite(byteArray);
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);
时光与爱终年不遇 2024-07-17 17:01:20

首先,您必须了解哑剧类型。 一旦知道了这一点,您就可以设置 Response.ContentType 属性。 之后,只需使用 Response.BinaryWrite() 即可。 如果不首先设置 ContentType 属性,客户端几乎没有机会正确打开文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文