禁止直接访问,但允许从页面内下载文件 [英] Preventing direct access but allowing file downloads from within a page

查看:59
本文介绍了禁止直接访问,但允许从页面内下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,上面有一些文件,只有在用户按照我的条件具有足够的访问权限时,我才希望下载该文件.

I'm developing a site that has a few files on it that I only want to be downloadable if the users have sufficient access on my terms.

基本上,我的页面上有一个下载链接,但是只有当用户具有正确的角色并且与数据库中的正确属性相关联时,才会显示并激活下载链接.

Basically, I have a page that has a download link on, but the download link will only be displayed and activated if the user has the correct roles and are associated with the correct properties in my database.

我面临的问题是我不希望用户直接访问文件,例如,如果他们访问www.mysite.com/downloads/myfile.pdf-我不希望他们访问能够保留该文件,尽管我确实希望能够在他们登录后允许他们下载该文件,并且我已检查它们是否符合我的自定义规则和规章.

The problem I am facing is that I don't want users to be able to access the file directly, for example if they go to www.mysite.com/downloads/myfile.pdf - I don't want them to be able to get hold of the file although I do want to be able to allow them to download it once they have logged in and I have checked that they fulfil my custom rules and regulations.

我本来打算那样做,但是我相信禁用了权限后我将无法这样做.

I was going to do it like so, but I believe with the permissions deactivated I won't be able to do so.

System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();

是否有可能实现我的目标?希望我已经对自己做了充分的解释.

Is it possible to achieve my aims? Hopefully I've explained myself sufficiently.

谢谢

推荐答案

我确定您使用的是正确的方法.

I am sure you at the correct way.

这是一个简单的例子:

Response.ContentType = "text/txt";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "file.txt");
Response.Write(file.ToString());
Response.End();

----编辑----还有其他好的示例:

---- EDIT ---- There are others good samples:

这篇关于禁止直接访问,但允许从页面内下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆