UPLOAD des fichiers pdf d'un poste client vers un serveur en asp
cs_madhatterx
Messages postés11Date d'inscriptionsamedi 28 novembre 2009StatutMembreDernière intervention21 juin 2012
-
27 juil. 2010 à 17:19
cs_madhatterx
Messages postés11Date d'inscriptionsamedi 28 novembre 2009StatutMembreDernière intervention21 juin 2012
-
28 juil. 2010 à 14:06
Bonjour,
J'ai trouvé plusieurs codes pour "uploader" des fichiers d'un client vers un serveur en ASP, j'ai fait ma cuisine avec tout ça et j'obtiens le code suivant:
-1ere étape:
transfert.asp
<%@ LANGUAGE= "VBSCRIPT"%>
<html>
<head>
<title>Transfert de fichier</title>
<link rel="stylesheet" type="text/css" href="http://10.0.0.241/itcinfo/feusty.css">
</head>
TRANSFERT DE FICHIER
<FORM METHOD ="POST" ENCTYPE="multipart/form-data" ACTION="upload.asp">
Sélectionnez un fichier à transférer:
,
----
</FORM>
</HTML>
-2eme étape:
upload.asp
<%@ Language=VBScript %>
<html>
<head>
<title>Transfert de fichier</title>
<link rel="stylesheet" type="text/css" href="http://10.0.0.241/itcinfo/feusty.css">
</head>
<%
Class FileUploader
Public Files
Private mcolFormElem
Private Sub Class_Initialize()
Set Files = Server.CreateObject("Scripting.Dictionary")
Set mcolFormElem = Server.CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
If IsObject(Files) Then
Files.RemoveAll()
Set Files = Nothing
End If
If IsObject(mcolFormElem) Then
mcolFormElem.RemoveAll()
Set mcolFormElem = Nothing
End If
End Sub
Public Property Get Form(sIndex)
Form = ""
If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex))
End Property
Public Default Sub Upload()
Dim biData, sInputName
Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos
Dim nPosFile, nPosBound
biData = Request.BinaryRead(Request.TotalBytes)
nPosBegin = 1
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
If (nPosEnd-nPosBegin) <= 0 Then Exit Sub
vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
nDataBoundPos = InstrB(1, biData, vDataBounds)
Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--"))
nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition"))
nPos = InstrB(nPos, biData, CByteString("name="))
nPosBegin = nPos + 6
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename="))
nPosBound = InstrB(nPosEnd, biData, vDataBounds)
If nPosFile <> 0 And nPosFile < nPosBound Then
Dim oUploadFile, sFileName
Set oUploadFile = New UploadedFile
nPosBegin = nPosFile + 10
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34)))
sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, ""))
nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:"))
nPosBegin = nPos + 14
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13)))
oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
nPosBegin = nPosEnd+4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin)
If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile
Else
nPos = InstrB(nPos, biData, CByteString(Chr(13)))
nPosBegin = nPos + 4
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2
If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin))
End If
nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds)
Loop
End Sub
'String to byte string conversion
Private Function CByteString(sString)
Dim nIndex
For nIndex = 1 to Len(sString)
CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1)))
Next
End Function
'Byte string to string conversion
Private Function CWideString(bsString)
Dim nIndex
CWideString =""
For nIndex = 1 to LenB(bsString)
CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1)))
Next
End Function
End Class
Class UploadedFile
Public ContentType
Public FileName
Public FileData
Public Property Get FileSize()
FileSize = LenB(FileData)
End Property
Public Sub SaveToDisk(sPath)
Dim oFS, oFile
Dim nIndex
If sPath "" Or FileName "" Then Exit Sub
If Mid(sPath, Len(sPath)) <> "" Then sPath = sPath & ""
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
If Not oFS.FolderExists(sPath) Then Exit Sub
Set oFile = oFS.CreateTextFile(sPath & FileName, True)
For nIndex = 1 to LenB(FileData)
oFile.Write Chr(AscB(MidB(FileData,nIndex,1)))
Next
oFile.Close
End Sub
Public Sub SaveToDatabase(ByRef oField)
If LenB(FileData) = 0 Then Exit Sub
If IsObject(oField) Then
oField.AppendChunk FileData
End If
End Sub
End Class
' Create the FileUploader
Dim Uploader, File
Set Uploader = New FileUploader
' This starts the upload process
Uploader.Upload()
' Check if any files were uploaded
If Uploader.Files.Count = 0 Then
Response.Write "Fichier non transféré."
Else
Response.Write "Transfert terminé avec succès!
"
' Loop through the uploaded files
For Each File In Uploader.Files.Items
' Save the file
File.SaveToDisk "C:\inetpub\wwwroot\itcinfo\References\PDF"
' Output the file details to the browser
Response.Write "Fichier transféré: " & File.FileName & "
"
Response.Write "Taille: " & File.FileSize & " bits
"
Response.Write "Type: " & File.ContentType & "
"
Next
End If
%>
</HTML>
Du poste client, j'arrive à uploader des fichiers ".asp", word, excel, access, ".png", ".jpeg", ".gif", ".zip", ".bmp", MAIS je n'arrive pas à transférer les fichiers de type ".txt", ".pdf", ".wma" ... le PDF étant le plus important pour mon appli.
...
Protected Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim status As String = ""
lblMessage.Text = ""
Dim fileOK As Boolean = False
If lstFiles.Items.Count 0 And filesUploaded 0 Then
lblMessage.Text = "Erreur un nom de fichier doit etre spécicié."
Exit Sub
Else
For fic As Integer = 0 To hif.Count - 1
If CType(hif.Item(fic), FileUpload).HasFile Then
Dim fileExtension As String = System.IO.Path.GetExtension(hif.Item(fic).PostedFile.FileName).ToLower()
Dim allowedExtensions As String() = {".jpg", ".jpeg", ".png", ".gif", ".pdf"}
For i As Integer = 0 To allowedExtensions.Length - 1
If fileExtension = allowedExtensions(i) Then
fileOK = True
End If
Next
If fileOK Then
Try
Dim iFile As FileInfo
iFile = New FileInfo(baseLocation & System.IO.Path.GetFileName(hif.Item(fic).PostedFile.FileName))
If iFile.Exists Then
lblMessage.Text &= "Le fichier " & System.IO.Path.GetFileName(hif.Item(fic).PostedFile.FileName) & " est déjà présent.
"
Else
CType(hif.Item(fic), FileUpload).PostedFile.SaveAs(baseLocation & System.IO.Path.GetFileName(hif.Item(fic).PostedFile.FileName))
filesUploaded += 1
End If
Catch ex As Exception
lblMessage.Text = "Erreur de sauvegarde du fichier " & System.IO.Path.GetFileName(hif.Item(fic).PostedFile.FileName) & "
" & ex.Message
End Try
End If
End If
Next fic
If filesUploaded > 1 Then
lblMessage.Text &= "Les " & filesUploaded & " fichiers ont été uploadés avec succès." + status
Else
lblMessage.Text &= "Le fichier a été uploadé." + status
End If
filesUploaded = 0
hif.Clear()
lstFiles.Items.Clear()
End If
End Sub
...