Re: Send and recive files

From: Vilmar Brazão de Oliveira (teste_at_teste.teste.teste)
Date: 02/13/04


Date: Fri, 13 Feb 2004 13:39:00 -0300

hi, take this full sample:
html part:
 <form method="post" action="" enctype="multipart/form-data"
name="frmUpLoad" onsubmit=" return Verificar();">
        <table width="100%" border="0" cellspacing="3" cellpadding="0">
          <tr>
            <td width="43%" height="38" align="right"> <font size="2"
face="Arial">
              <strong>Selecione o arquivo:&nbsp;&nbsp;</strong></font> </td>
            <td width="57%"> <input accept="image/jpeg" type="file"
name="filArquivo1" style="font-family: Arial; font-size: 8 pt;
background-color: #EBEBEB; font-weight: bold" size="30">
            </td>
          </tr>
          <tr>
            <td height="67">&nbsp;</td>
            <td><input name="Cancelar" type="reset" id="Cancelar" value="
Cancelar ">
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input name="Enviar"
type="submit" value=" Enviar ">
            </td>
          </tr>
        </table>
      </form>

asp part:
<%@Language="VBScript"%>
<%
Option Explicit

'Variáveis desta pág. usadas p/ upload da imagem.
Dim Contador, Tamanho, Tamanho2
Dim ConteudoBinario, ConteudoTexto
Dim Delimitador, Posicao1, Posicao2
Dim ArquivoNome, ArquivoConteudo, PastaDestino
Dim objFSO, objArquivo
Dim Operacao_OK

'Monta nome do diretório no padrão dos diretórios de prédios.
PastaDestino = "c:\inetpub\wwwroot\upload\"

'Verifica se pasta do prédio já existe. Se não existir irá criá-la.
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(PastaDestino) Then
 objFSO.CreateFolder(PastaDestino)
End If

Set objFSO = nothing

'Response.Write "Session(logado): " & Session("logado") & "<br>PastaDestino:
" & PastaDestino & "<br>"
'Response.End()
%>
<html>
<head>
<title>A&ccedil;&atilde;o upload_sem_componentes.asp</title>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%
'***** Determina o Tamanho do Conteúdo *****
Tamanho = Request.TotalBytes
Tamanho2 = eval(Tamanho)
if Tamanho2 > 102400 then '102400 Bytes = 100 KBytes
 Response.write "<script language='JavaScript'>alert('Favor não enviar
arquivos maiores do que 100 KBytes.'); "
 Response.write "history.go(-1);</script>"
 Response.End()
end if

'***** Obtém o Conteúdo no Formato Binário *****
ConteudoBinario = Request.BinaryRead(Tamanho)

'***** Transforma o Conteúdo Binário em String *****
For Contador = 1 To Tamanho
 ConteudoTexto = ConteudoTexto & Chr(AscB(MidB(ConteudoBinario, Contador,
1)))
Next

'***** Determina o Delimitador de Campos *****
Delimitador = Left(ConteudoTexto,InStr(ConteudoTexto, vbCrLf)-1)

'***** Percore a String Procurando os Campos *****
'***** Identifica os Arquivos e Grava no Disco *****
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Posicao1 = InStr(ConteudoTexto, Delimitador) + Len(Delimitador)
Do While True
 ArquivoNome = ""
 Posicao1 = InStr(Posicao1, ConteudoTexto, "filename=")
 If Posicao1 = 0 Then
  Exit Do
 Else
  '***** Determina o Nome do Arquivo *****
  Posicao1 = Posicao1 + 10
  Posicao2 = InStr(Posicao1, ConteudoTexto, """")
  For Contador = Posicao2-1 To Posicao1 Step -1
   If Mid(ConteudoTexto, Contador, 1) <> "\" Then
    ArquivoNome = Mid(ConteudoTexto, Contador, 1) & ArquivoNome
   Else
    Exit For
   End If
  Next

  '***** Determina o Conteúdo do Arquivo *****
  Posicao1 = InStr(Posicao1, ConteudoTexto, vbCrLf & vbCrLf) + 4
  Posicao2 = InStr(Posicao1, ConteudoTexto, Delimitador) - 2
  ArquivoConteudo = Mid(ConteudoTexto, Posicao1, Posicao2-Posicao1+1)
' Response.Write "<b>ArquivoNome:</b> " & ArquivoNome
' Response.Write "<br><b>ArquivoConteudo:</b><br>" & ArquivoConteudo
' Response.End()

  '***** Grava o Arquivo *****
  If ArquivoNome <> "" Then
   Set objArquivo = objFSO.CreateTextFile(PastaDestino & ArquivoNome, True)
   objArquivo.WriteLine ArquivoConteudo
   objArquivo.Close
   Response.write "<script language='JavaScript'>alert('Seu arquivo foi
enviado com sucesso !!!'); "
   Response.write "history.go(-1);</script>"
   Operacao_OK = "sim"
   Set objArquivo = Nothing
  End If
 End If
Loop
Set objFSO = Nothing
%>
</body>
</html>
"Mrc" <anonymous@discussions.microsoft.com> escreveu na mensagem
news:588D34FD-85BA-4BD4-ADEF-D12A2B2CFC5B@microsoft.com...
> I would like to use only standard component just present on IIS. I can use
FSO if necessary.
> I've created a submit form where the user can select a file to upload.
> When the user press the button for submit the selected file, how my asp
page can read this file? This file are first saved on a temp dir by a
server?
> After my asp page have readed this file, I have to send a response file
(binary) to the client. I don't want to use a link (<a href=...>) because
this file must not be accessible by other user.
> How can i send file to the client? I have to specify mime type on header?
> I have to read a Chunk of file with FSO and send this Chunk to a client?
How can I send this Chunk?
>
>
> ----- Roland Hall wrote: -----
>
> "Mrc" wrote:
> : Note: This is my first ASP page :-)
> : I would use Vbs script on .asp page and IIS (not asp.NET)
> : I need to send and recive files.
> :
> : - Anyone have examples?
> : - When I have to send file from server to client, I need to read a
pice of
> file and send it to client? Exist any IIS function that accept file
name and
> path and send it to client?
>
> Hi Mrc...
>
> Can you clarify 'send and receive files'? Normally the server
provides HTML
> and client-side script to the client, after ASP script has been
processed.
> The browser parses and renders the results of the code received. If
the
> user needs a file, then it can be as simple as a link: <a
> href="somefile.ext">somefile.ext</a>. The user can right-click on
the link
> and choose Save Target As... and then browse to a specific path on
their
> drive to store the file.
>
> If the client needs to upload files to the server, then several
things are
> required.
>
> 1. The server needs an ASP component or you can use pure ASP code if
a
> component is not available (installed on the server).
> 2. A form with an <input type=file...> tag will allow the user to
browse
> their local drive and select a file to be upload. Then they submit
the form
> calling for the ASP script with the ActiveX component or the pure ASP
> upload.
> 3. The upload directory will have to have appropriate rights assigned
to the
> anonymous user (the account the user uses) so the file can be written
to the
> server.
> 4. Generally after that you will want to use FSO (FileSystemObject)
to move
> the file a directory (folder) where it can be made available for
others ( if
> this part needs to be automated). Otherwise the site Admin could use
> numerous ways to manage the files, i.e. web site control panel, IDE
> interface like Visual Studio, custom ASP interface, etc.
>
> You can download an archive of two ASP scripts that will test for ASP
> components installed on a web server and to test for server-side
email
> components.
>
> http://www.pensaworks.com/prg_com.asp
>
> Here is a link if you want to use pure ASP for your upload process.
> http://www.asp101.com/articles/jacob/scriptupload.asp
>
> Here is a script using FSO to move a file from one directory to
another on
> the server.
>
http://www.devguru.com/Technologies/vbscript/quickref/filesystemobject_movef
ile.html
>
> HTH...
>
> --
> Roland Hall
> /* This information is distributed in the hope that it will be
useful, but
> without any warranty; without even the implied warranty of
merchantability
> or fitness for a particular purpose. */
> Technet Script Center -
http://www.microsoft.com/technet/scriptcenter/
> WSH 5.6 Documentation -
http://msdn.microsoft.com/downloads/list/webdev.asp
> MSDN Library - http://msdn.microsoft.com/library/default.asp
>
>
>