我正在开发在线图书采购的应用程序
我给下载PDF格式的书后,支付了钱,所以我想确保我的PDF从直接下载。
问题:
1)下载PDF时,我如何隐藏文件夹URL?
2) 不允许用户直接访问文件夹或PDF的任何安全代码
3)当点击下载时,它将直接下载PDF,而不是在浏览器中打开,然后下载
发布于 2019-12-17 04:39:55
解决这个问题有很多种方法,所以我会从臀部开始,提出一些可能对你有帮助的想法。
推荐
首先,您不会希望显式地公开应用程序中的任何文件或文件夹。如果你想直接为他们服务,让你的应用程序为你服务。有很多事情你可以考虑:
一个潜在的工作流可能会出现这样的情况:
用户选择项目到purchase/download.
。
如果您确实选择了一个链接供用户显式下载该文件,请确保您没有公开该文件本身,而是提供了一个端点来处理确定访问并最终按如下方式提供该文件:
// You could consider writing a custom attribute that would store which files a given
// user had access to (via claims, etc.) but make sure the endpoint requires authentication
[Authorize]
public FileResult DownloadFile(Guid fileId)
{
// Here you could explicitly check in your database to ensure the user had access
// to the requested file, otherwise revoke the request
if (CanAccessFile(context.UserId, fileId))
{
// If they can access the file, then serve it from the appropriate location
return File(...);
}
}同样,您还可以支持这样的场景:只要访问链接,用户就可以通过传递请求的文件以及令牌来下载文件:
// You wouldn't necessarily need authentication here because the token and
// requested file should be enough
public FileResult DownloadFileWithToken(Guid downloadToken, Guid fileId)
{
// Here you would just check your database to ensure that the token was
// valid for the specific file and if so, allow the user to download it
}你的问题
下载
时,我如何隐藏文件夹URL?
不要直接公开文件,这样用户就可以访问它。
2)任何不允许用户直接访问文件夹或PDF的安全代码?
再次-不允许直接访问任何文件夹或文件。如果您在付款后提供这些文件,那么您没有理由在站点/应用程序上显式地公开这些文件。
3)当点击下载时,它将直接下载PDF而不是打开浏览器,然后下载?
您可以通过HTML download属性完成此行为(无论您选择如何处理此过程):
<a href="path" download>Download</a>发布于 2019-12-17 04:41:11
_ Your project
|_ wwwroot
|_ PDF_folder
|__ file_01.pdf
|__ file_02.pdf通过这种方式,用户不能通过URL访问文件。
example.com/pdf_folder/file_01.pdf,此路径将响应404状态代码。
public ActionResult Download()
{
// code goes here...
return File(...);
}https://stackoverflow.com/questions/59367599
复制相似问题