ago
28
2009

Script em ASP para listagem de arquivos

Script em ASP para listagem de arquivos

Script em ASP para listagem de arquivos

Disponibilizamos aqui um script em ASP (Active Server Pages) simples para listagem de arquivos em um diretório. Utilize à vontade, lembrando que este script pode ser expandido para listagem de diretórios, multinavegação, upload de arquivos, etc.

Tente reescrevê-lo em .Net…

Início em VBScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< %
Option Explicit
Response.Buffer = true

Dim acao, arquivo, mensagem
Dim objFSO, Diretorio, arquivos
Dim i, color, arr_data

acao    = UCase(Trim(Request("acao")))
arquivo = Trim(Request("arquivo"))

if acao = "EXCLUIR" then
    set objFSO = Server.CreateObject("scripting.filesystemobject")
   
    if objFSO.FileExists(Server.MapPath(".\") & "\" & arquivo) then
        objFSO.DeleteFile(Server.MapPath(".\") & "\" & arquivo)
        mensagem = "Arquivo """ & Server.MapPath(".\") & "\" & arquivo & """excluído com sucesso!"
    else
      mensagem = "O arquivo especificado para exclusão não existe!"
    end if
   
    set objFSO = nothing
end if
%>

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<table id="main" cellspacing="0" cellpadding="5" width="728" align="center">
  <thead bgcolor="#E6E6E6">
    <th height="25" class="cabecalho" align="left">Nome</th>
    <th height="25" class="cabecalho" align="right">Tamanho</th>
      <th height="25" class="cabecalho" align="left">Tipo</th>
    <th height="25" class="cabecalho" align="left">Data de Modificação</th>
    <th height="25" class="cabecalho" align="center">Ação</th>
  < %

set objFSO = Server.CreateObject("scripting.filesystemobject")
set Diretorio = objFSO.GetFolder(Server.MapPath(".\"))
set arquivos  = Diretorio.files
%>
  < %
    if Diretorio.Size > 0 then
      i = 0
      color = ""
      for each arquivo in arquivos
        i = i + 1
        if i mod 2 = 0 then
          color = "#F6F6F6"
        else
          color = "#FFFFFF"
        end if
        arr_data = split(arquivo.DateCreated," ")
        if Right(arquivo.Name,4) <> ".asp" then
          Response.Write "<tr bgcolor=""" & color & """>" & _
                "<td align=""left"">" & arquivo.Name & "</td>"& _
                "<td align=""right"">" & FormatNumber(arquivo.Size / 1024 , 2)& " KB </td>" & _
                "<td align=""left"">" & arquivo.Type & "</td>"& _
                "<td align=""left"">" & arr_data(0) & " " & arr_data(1) & "</td>"& _
                "<td align=""center""><input type=""button"" onclick=""javascript:download('"& arquivo.Name &"');"" value=""Download""/></td>" &_
                "</tr>"
        else
          i = i + 1
        end if
    next
    set objFSO = nothing
    else
    %>
    <tr>
        <td height="50" colspan="5" align="center" bgcolor="#FFFFFF" style="border-bottom:#FFCC00 2px solid;"><img src="image/alerta_32x32.png" width="32" height="32" border="0" align="middle"/>&nbsp;&nbsp;Não existem arquivos no diretório&nbsp;"<b>< %=Server.MapPath(".\")%></b>"!</td>
    </tr>
    < %
    end if
  %>
</thead></table>

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<style>
#main {
  overflow: auto;
  height: 25px;
  border: 1px solid #AAAAAA;
  background-color: white;
  font-family: Tahoma;
  font-size: 11px;
  color: #000000;
}

.cabecalho {
  border-bottom:#CCCCCC 1px solid;
  border-top:#FFFFFF 1px solid;
  border-left:#FFFFFF 1px solid;
  border-right:#CCCCCC 1px solid;
}

input, select, textarea {
  font-family: Tahoma;
  font-size: 11px;
  color: #000000;
}
</style>

Javascript

1
2
3
4
5
6
7
8
9
<script language="javascript">
<!--
function download(arquivo){
    if (confirm('Deseja realmente baixar o arquivo selecionado?')){
            document.location.href=arquivo;
    }
}
//-->
</script>

Download

Posts relacionados

About the Author: Edu Nicácio

Analista de Sistemas com experiência em sistemas de médio e grande porte em empresas de Telecom, Seguros e Automobilística. Possui mais de sete anos de experiência em desenvolvimento de sistemas Web, cliente-servidor, multi-camadas e desktop, trabalhando com tecnologias como .Net (C#), Java, Delphi, ASP, JavaScript, XML, CSS, XHTML, SQL, Transact/SQL, PL/SQL, PG/SQL, MySQL, Unix/Linux e ShellScript. Quando não está estudando ou desenvolvendo alguma coisa nova, gosta de passar o tempo com a esposa e os amigos.

Deixe um comentário

Spam protection by WP Captcha-Free

Publicidade