如何在 Telerik RadEditor 上强制下载文件?

发布于 2024-10-07 00:31:15 字数 120 浏览 0 评论 0原文

我正在 Telerik RadEditor 中使用文档管理器。

一旦我上传 .txt 文件并单击该链接,它就会在浏览器上打开而不是下载。如何在不进入 .htaccess 或其他服务器更改的情况下强制下载该文件?

I am using Document Manager in Telerik RadEditor.

Once I upload .txt file and click on that link, it opens on the browser instead of downloading it. How to force download that file without going in to .htaccess or other server changes?

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

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

发布评论

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

评论(1

故事与诗 2024-10-14 00:31:15

简短的回答是:单独使用 RadEditor 是不行的。

为了使浏览器可查看的文件类型作为下载服务,您必须使用“内容处置”类型的“附件”将其发送到客户端的浏览器。这样做相当简单,但它需要超出 RadEditor 范围的服务器端代码。

var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/path/to/file.txt"));
Response.AddHeader("Content-Type", "text/plain");
Response.AddHeader("Content-Displosition", "attachment;filename=file.txt;size=" + bytes.Length);
Response.Flush();
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

除非您想编写一个特定的处理程序来提供相关文件,否则您唯一的选择是指示用户必须“右键单击 > >”。将链接另存为...”在文本文件链接上。

Short answer is: using RadEditor alone you can't.

In order to make a browser-viewable file type be served as a download you must send it to the client's browser with a 'Content-Disposition' type of 'attachment'. Doing so is fairly simple, however it requires server-side code that would be outside of the scope of RadEditor.

var bytes = System.IO.File.ReadAllBytes(Server.MapPath("~/path/to/file.txt"));
Response.AddHeader("Content-Type", "text/plain");
Response.AddHeader("Content-Displosition", "attachment;filename=file.txt;size=" + bytes.Length);
Response.Flush();
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

Unless you want to write a specific handler for serving the file in question, your only option is to instruct users that they must 'Right-Click > Save Link As...' on your text file link.

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