Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to store an file to an folder using asp!!URGENT

Status
Not open for further replies.

yogesh

Programmer
Jun 22, 2000
14
0
0
US
hi guys let me know how to store an file in an server i:e how to upload the file fom client side on the web folder

thanks
yogesh [sig][/sig]
 
####################################
Pour ça, tu copies les 3 fonctions suivantes dans un fichier genre 'uploadfunction.inc'

<%
REM *** Isole le nom du fichier du chemin complet ***
Function GetFileName(FullPath)
Dim Pos, PosF
PosF = 0
For Pos = Len(FullPath) To 1 Step -1
Select Case Mid(FullPath, Pos, 1)
Case &quot;/&quot;, &quot;\&quot;: PosF = Pos + 1: Pos = 0
End Select
Next
If PosF = 0 Then PosF = 1
GetFileName = Mid(FullPath, PosF)
End Function

REM *** Convertie une chaine UTF en unicode (abus de language) ***
Function getString(StringBin)
getString =&quot;&quot;
For intCount = 1 To LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function

REM *** Réciproque de la fonction d'avant ***
Function getByteString(StringStr)
For i = 1 To Len(StringStr)
char = Mid(StringStr, i, 1)
getByteString = getByteString & chrB(AscB(char))
Next
End Function

REM *** Construit l'objet Scripting.Dictionnary intégrant l'ensemble des éléments du form ***
Function BuildUploadRequest(RequestBin)
Dim scrDict
Set scrDict = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
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(&quot;--&quot;)))
Dim UploadControl
Set UploadControl = CreateObject(&quot;Scripting.Dictionary&quot;)
Pos = InstrB(BoundaryPos,RequestBin, getByteString(&quot;Content-Disposition&quot;))
Pos = InstrB(Pos,RequestBin,getByteString(&quot;name=&quot;))
PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile=InstrB(BoundaryPos,RequestBin,getByteString(&quot;filename=&quot;))
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 &quot;FileName&quot;, FileName
Pos = InstrB(PosEnd,RequestBin,getByteString(&quot;Content-Type:&quot;))
PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add &quot;ContentType&quot;,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 &quot;Value&quot; , Value
scrDict.Add Name, UploadControl
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop
Set BuildUploadRequest = scrDict
Set scrDict = Nothing
End Function
%>


###########################
Enfin tu récupères les éléments du POST en faisant :
<%str = Dict.Item(&quot;formfield-name&quot;).Item(&quot;Value&quot;)%>

Exemple :
HTML-----------
<form name=&quot;tralala&quot; action=&quot;upload.asp&quot; method=&quot;POST&quot; enc-type=&quot;multiform/part-data&quot;>
<Input type=&quot;text&quot; name=&quot;nom&quot;>
<Input type=&quot;file&quot; name=&quot;fichier&quot;>
</form>

Upload.asp------------
<%dim tBytes, binData, Dict
tBytes = Request.TotalBytes
binData = Request.BinaryRead(tBytes)
Set Dict = Server.CreateObject(&quot;Scripting.Dictionary&quot;)
Set Dict = BuildUploadRequest(binData)
%>

<font ...>NOM = <%=Dict.Item(&quot;nom&quot;).Item(&quot;Value&quot;)%></font>
<font ...>NOM = <%=Dict.Item(&quot;fichier&quot;).Item(&quot;Content-Type&quot;)%></font>

' Pour enregistrer le fichier dans un répertoire :

Set fichierconn = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set file = fichierconn.CreateTextFile(&quot;c:\tmp\&quot; & GetFileName(Dict.Item(&quot;fichier&quot;).Item(&quot;FileName&quot;)))
file.write(getString(Dict.Item(&quot;fichier&quot;).Item(&quot;Value&quot;)))
file.close
Set file = nothing
Set fichierconn = nothing

##################
Voila :) !!!!

Bon, normalement pour que tout marche bien, il faut tester les erreurs et tout ça... En plus, pour les gros fichiers (>75Ko), il faudrait que tu fractionnes le flux &quot;binData&quot; en plusieurs morceaux parce qu'avec tout les appels de fonctions le serveur IIS se met à ramer terriblement (trop de transfert de données). Et si t'as un probleme, tu peux m'écrire à anqh@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top