UPLOAD des fichiers pdf d'un poste client vers un serveur en asp

cs_madhatterx Messages postés 11 Date d'inscription samedi 28 novembre 2009 Statut Membre Dernière intervention 21 juin 2012 - 27 juil. 2010 à 17:19
cs_madhatterx Messages postés 11 Date d'inscription samedi 28 novembre 2009 Statut Membre Dernière intervention 21 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.

Merci de me donner un coup de pouce.

4 réponses

PascalCmoa Messages postés 239 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 17 janvier 2013 5
27 juil. 2010 à 17:50
Bonjour,

Pourquoi ne pas avoir fait quelque chose comme ci-dessous, ce bout de code fonctionne parfaitement:

...



<ContentTemplate>
<fieldset>
<legend>
</legend>
,
,
,

----

,
,

----

,
,

----

<table border="0" cellpadding="0" cellspacing="0">
----,

</td>

----





</td>
</tr>
</table>


</fieldset>
</ContentTemplate>
<Triggers>



</Triggers>



...


...
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
...



PascalCmoa
email: PascalCmoa
0
cs_madhatterx Messages postés 11 Date d'inscription samedi 28 novembre 2009 Statut Membre Dernière intervention 21 juin 2012
28 juil. 2010 à 08:00
Merci d'avoir répondu si vite! :)

Le problème, c'est que mon appli est pour un Intranet tout en ASP, et je ne connais pas encore ASP.NET.

Je vais essayer quand même avec ton code.
0
cs_madhatterx Messages postés 11 Date d'inscription samedi 28 novembre 2009 Statut Membre Dernière intervention 21 juin 2012
28 juil. 2010 à 08:18
indications supplémentaires:
je bosse sur windows server 2008, avec IIS7 et SQL Server 2008.
Si ça peut faire avancer le schmilblick.
0
cs_madhatterx Messages postés 11 Date d'inscription samedi 28 novembre 2009 Statut Membre Dernière intervention 21 juin 2012
28 juil. 2010 à 14:06
Je n'ai pas réussi à paramétrer ton code pour mon appli
Aurais-tu une variante en ASP classic? stp
0
Rejoignez-nous