使用AngularJS如何检查文件夹中是否存在PDF文件

使用AngularJS如何检查文件夹中是否存在PDF文件

本文介绍了使用AngularJS如何检查文件夹中是否存在PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学生名单.
对于某些学生,我必须显示一个链接,单击此链接后,他们的进度卡应该会加载(这是一个.pdf文件).对于其他用户,不应显示该链接.

I have a list of students.
For some students I have to show a link, and on clicking this link their progress card should load (it is a .pdf file). For others, the link should not be shown.

我是通过给他们的进度卡提供相同的学生ID来完成的,如果是这个特定学生,则显示链接;否则,请勿显示链接.我只能以这种方式为5或6个学生做这件事.我不能为1000名学生做到这一点.

I have done this by giving the same student id to their progress card and if it is this particular student then show the link; otherwise, don't show the link. I can do it this way for only 5 or 6 students. I cant do this for 1000 students.

以下是我的AngularJS代码:

The following is my AngularJS code:

if (response.student_id == 'SD0001' || response.student_id == 'SD0002' || response.student_id == 'SD0004') {
    $scope.pdfData = true;
    $scope.StudentPDFFile =  response.student_id+'.pdf';
} else {
    $scope.pdfData = false;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<body id="ng-app" ng-app>
    <div ng-switch on="pdfData">
        <div ng-switch-when="false"></div>
        <div ng-switch-when="true">
            <span class="font-10">Download Performance Report</span>
            <a href="http://www.example.com/s-pdf/{{StudentPDFFile}}" target="_blank">­ click here</a>
        </div>
    </div>
</body>

这里我还没有指定控制器,这是骨架功能正在为我工​​作.

Here I haven't specified the controller, this is skeleton functionality is working for me.

对于1000条记录,我该怎么做?

For 1000 records how I can do this?

推荐答案

只要您与pdf文件位于同一个域中,就可以发出http请求来确定文件是否存在.

You can make a http request to determine if the file exists as long as you are on the same domain as the pdf file.

对于给定的学生证(在您的代码中为response.student_id),它看起来像这样:

For a given student ID (which in your code appears to be response.student_id) it looks like this:

if(response.student_id) {
    var url = 'http://www.example.com/s-pdf/' + response.student_id + '.pdf';
    var request = new XMLHttpRequest();
    request.open('HEAD', url, false);
    request.send();
    if(request.status == 200) {
        $scope.pdfData = true;
        $scope.StudentPDFFile =  response.student_id+'.pdf';
    } else {
        $scope.pdfData = false;
    }
} else {
    $scope.pdfData = false;
}

'HEAD'是确保文件存在所需的全部内容.您也可以使用'GET',但在这种情况下似乎不需要整个文件.

The 'HEAD' is all that is necessary to ensure that the file exists; you could also use 'GET' but it doesn't seem like you need the whole file in this situation.

这篇关于使用AngularJS如何检查文件夹中是否存在PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:36