•  
     

ASP - Como fazer upload sem componentes

Páginas necessarias:

Upload_funcoes.asp

Código: Selecionar todos

<%
' Upload Sem componentes ---------------------------------------
Sub BuildUploadRequest(RequestBin)
on error resume next

PosBeg = 1
PosEnd = InStrB(PosBeg, RequestBin, getByteString(Chr(13)))
boundary = MidB(RequestBin, PosBeg, PosEnd - PosBeg)
BoundaryPos = InStrB(1, RequestBin, boundary)

Do Until (BoundaryPos = InStrB(RequestBin, boundary & getByteString("--")))

Dim UploadControl
Set UploadControl = CreateObject("Scripting.Dictionary")

Pos = InStrB(BoundaryPos, RequestBin, getByteString("Content-Disposition"))
Pos = InStrB(Pos, RequestBin, getByteString("name="))
PosBeg = Pos + 6
PosEnd = InStrB(PosBeg, RequestBin, getByteString(Chr(34)))
Name = getString(MidB(RequestBin, PosBeg, PosEnd - PosBeg))
PosFile = InStrB(BoundaryPos, RequestBin, getByteString("filename="))
PosBound = InStrB(PosEnd, RequestBin, boundary)

If PosFile <> 0 And (PosFile < PosBound) Then
PosBeg = PosFile + 10
PosEnd = InStrB(PosBeg, RequestBin, getByteString(Chr(34)))
FileName = getString(MidB(RequestBin, PosBeg, PosEnd - PosBeg))
UploadControl.Add "FileName", FileName
Pos = InStrB(PosEnd, RequestBin, getByteString("Content-Type:"))
PosBeg = Pos + 14
PosEnd = InStrB(PosBeg, RequestBin, getByteString(Chr(13)))
ContentType = getString(MidB(RequestBin, PosBeg, PosEnd - PosBeg))
UploadControl.Add "ContentType", ContentType
PosBeg = PosEnd + 4
PosEnd = InStrB(PosBeg, RequestBin, boundary) - 2
Value = MidB(RequestBin, PosBeg, PosEnd - PosBeg)
Else
Pos = InStrB(Pos, RequestBin, getByteString(Chr(13)))
PosBeg = Pos + 4
PosEnd = InStrB(PosBeg, RequestBin, boundary) - 2
Value = getString(MidB(RequestBin, PosBeg, PosEnd - PosBeg))
End If

UploadControl.Add "Value", Value
UploadRequest.Add Name, UploadControl
BoundaryPos = InStrB(BoundaryPos + LenB(boundary), RequestBin, boundary)
Loop
End Sub

Function getByteString(StringStr)
For i = 1 To Len(StringStr)
Char = Mid(StringStr, i, 1)
getByteString = getByteString & ChrB(AscB(Char))
Next
End Function

Function getString(StringBin)
getString = ""
For intCount = 1 To LenB(StringBin)
getString = getString & Chr(AscB(MidB(StringBin, intCount, 1)))
Next
End Function

' Fim upload sem Componentes -----------------------------------------
%>


Pagina Upload_form.asp

Código: Selecionar todos

<!-- #include file = "upload_funcoes.asp" -->
<table>
<form name="form1" action="upload_form2.asp" method="post" enctype="multipart/form-data">
<tr>
<td>Nome:</td>
<td><input type="text" name="nome" size="20"></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" size="20"></td>
</tr>
<tr>
<td>Foto:</td>
<td><input type="file" name="foto" size="14"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Enviar">
</td>
</tr>
</form>	
</table>


Pagina upload_form2.asp

Código: Selecionar todos

<!-- #include file = "upload_funcoes.asp" -->
<%
' Chamando Funções, que fazem o Upload funcionar
byteCount = Request.TotalBytes
RequestBin = Request.BinaryRead(byteCount)
Set UploadRequest = CreateObject("Scripting.Dictionary")
BuildUploadRequest RequestBin

' Recuperando os Dados Digitados ----------------------
nome = UploadRequest.Item("nome").Item("Value")
email = UploadRequest.Item("email").Item("Value")

' Tipo de arquivo que esta sendo enviado
tipo_foto = UploadRequest.Item("foto").Item("ContentType")

' Caminho completo dos arquivos enviados
caminho_foto = UploadRequest.Item("foto").Item("FileName")

' Nome dos arquivos enviados
nome_foto = Right(caminho_foto,Len(caminho_foto)-InstrRev(caminho_foto,"\"))

' Conteudo binario dos arquivos enviados
foto = UploadRequest.Item("foto").Item("Value")

' pasta onde as imagens serao guardadas
pasta = Server.MapPath("imagens/")
nome_foto = "/"&nome_foto

' pasta + nome dos arquivos
cfoto = "imagens/lojas" + nome_foto

' Fazendo o Upload do arquivo selecionado
if foto <> "" then
Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
Set MyFile = ScriptObject.CreateTextFile(pasta & nome_foto)
For i = 1 to LenB(foto)
MyFile.Write chr(AscB(MidB(foto,i,1)))
Next
MyFile.Close
end if

' Conecta-se ao Banco de Dados
url_conexao = Server.MapPath("banco.mdb")
set conexao = Server.CreateObject("ADODB.Connection")
conexao.open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="&url_conexao	

' cadastra os dados no banco de dados
sql = "Insert into lojas (nome,email,foto) values ('"& nome &"', '"& email &"', '"& cfoto &"')"
Conexao.Execute(sql)

' Mostra Mensagem de Confirmação na Tela
Response.write "Dados Cadastrados com Sucesso!"

' Redireciona após 5 segundos
response.write "<br><br>você será redirecionado em 5 segundos..<br>"
response.write "<meta http-equiv='refresh' content='5; url=index.asp'/>"
%>