FORUM Syndrome-OC - Jacky-PC


  Programmation


  Autre


  [asp] formulaire, upload fichier, insert db

 




1 utilisateur anonyme et 6 utilisateurs inconnus

 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[asp] formulaire, upload fichier, insert db

n°4779
nicodache
marmotte en chocolat concept ©
Posté le 09-01-2005 à 23:23:59  profilanswer
 

bon, z'explique le bazar :
 
je dois remettre un site web demain, j'ai fait un bon gros bout qui devrait satisfaire le prof, mais je voudrais en faire un peu plus, histoire d'etre sur que je réussisse ;)
 
la actuellement, j'ai une base de donnée avec une seule table, qui contient une clé (string) ,un libellé (string), un nom (string), et un lien relatif (string aussi), celui-ci pointant vers un fichier du filesystem, dans un des sous rep de la racine du serveur web
 
le concours actuellement est d'insérer un nouveau tuple dans la db, en uploadant du même coup le fichier sur le serveur, dans le bon rep.
 
le probleme est que je débute en asp, et que j'ai jamais fait de formulaires...
 
donc si qqun se sent d'humeur à m'aider pour demain avant 10h :D
 
 
je vous tape vite fait mon code html, vu que l'asp est totalement foiré :D
 
si qqun pouvait me taper un exemple d'uploade de fichier et d'insertion dans une db sql en php, jsp, asp, cgi, ou n'importe quoi du genre, ca serait sympa :D
 
 
code html :

Code :
  1. <html>
  2.   <head>
  3.     <title>Administration</title>
  4.     <link media="screen" href="../css/screen.css" type="text/css" rel="stylesheet" />
  5.   </head>
  6.   <body>
  7.     <h2>Insertion d'un nouveau cours</h2>
  8.     <div align="center">
  9.       <form method="post" enctype="multipart/form-data" runat="server">
  10.         <table summary="des trucs" border="1">
  11.           <tr>
  12.             <td align="right">Libellé du cours (requis) :</td>
  13.             <td><textarea id="libelle"></textarea></td>
  14.           </tr>
  15.           <tr>
  16.             <td align="right">Professeur (optionnel) :</td>
  17.             <td><input id="prof" type="text" /></td>
  18.           </tr>
  19.           <tr>
  20.             <td align="right">Fichier de description du cours (requis):</td>
  21.             <td><input id="fichier" type="file" runat="server" /></td>
  22.           </tr>
  23.           <tr>
  24.             <td align="right">Identifiant du cours (requis) :</td>   
  25.             <td><input id="id" type="text" /></td>
  26.           </tr>
  27.         </table>
  28.         <input onclick="putCours(id.text, libelle.text, prof.text, fichier.text)" type="submit" value="Submit" />
  29.         <input type="reset" value="Reset" />
  30.       </form>
  31.     </div>
  32.     <form runat="server">
  33.       <input type="submit" value="Signout" runat="server" onserverclick="Signout_Click" />
  34.     </form>
  35.   </body>
  36. </html>


---------------
modérateur inside [:nicodache] plankaivoo [:nicodache] - ici powered - Je roule en micra 1l 55cv et je t'emmerde :o
mood
Google
Posté le 09-01-2005 à 23:23:59  profilanswer
 

n°4783
Dr Lous
I see old password
Posté le 10-01-2005 à 06:08:01  profilanswer
 

Code :
  1. <%@Language="VBScript" %>
  2. <%
  3.     Application("Root" ) = "C:\inetpub\wwwroot\"
  4.    
  5.     DIM size
  6.     size = Request.TotalBytes
  7.     Response.Write size & " octets envoyés<BR>"
  8.     IF size > 0 THEN
  9. '-----------------------------------------------------------
  10. '-Transformation des données à sauvegarder------------------
  11. '-En effet les fonctions VbScript travaillent en Unicode----
  12. '-Le flot de caractères de la requête HTTP est en ASCII-----
  13. '-----------------------------------------------------------
  14. temp = Request.BinaryRead(size)
  15. theRequestString = ""
  16. Response.Write "<PRE Style=""background: #E0E0E0"">"
  17. FOR i = LBound(temp)+1 to UBound(temp)-1
  18.     myChar = Chr(AscB(MidB(temp,i+1,1)))
  19.     Response.write myChar
  20.     theRequestString = theRequestString & myChar
  21. NEXT
  22. Response.Write "</PRE>"
  23. '-----------------------------------------------------------
  24. '-Récupération du nom du fichier----------------------------
  25. '-----------------------------------------------------------
  26. pos = InStr(1, theRequestString, "filename=", 0) + 10
  27. begin = pos
  28. DO  ' Il faut supprimer tout ce qui préfixe le nom du fichier
  29.     theChar = Mid(theRequestString,pos,1)
  30.     IF theChar = """" THEN EXIT DO
  31.     IF theChar = "/" OR theChar = "\" THEN begin = pos+1
  32.     pos = pos + 1
  33. LOOP
  34. filename = Mid(theRequestString, begin, pos-begin)
  35. Response.Write "Nom du fichier à sauvegarder : " & filename
  36. '-----------------------------------------------------------
  37. '-Récupération des données à sauvegarder--------------------
  38. '-----------------------------------------------------------
  39. firstReturnPos = InStr(1,theRequestString,vbCrLf,0)
  40. doubleReturnPosition = InStr(firstReturnPos,theRequestString, _
  41.                                      vbCrLf & vbCrLf,0)
  42. size = Len(theRequestString) - doubleReturnPosition -  firstReturnPos - 8
  43. theRequestString = Mid(theRequestString, doubleReturnPosition+4, size)
  44. 'Response.Write "<PRE Style=""background: #E0E0E0"">" _
  45.         '               & theRequestString & "</PRE>"
  46. '-----------------------------------------------------------
  47. '-Sauvegarde des données à sauvegarder----------------------
  48. '-----------------------------------------------------------
  49. SET fs = Server.CreateObject("Scripting.FileSystemObject" )
  50. SET file = fs.CreateTextFile(Application("Root" ) & filename, True)
  51. file.write theRequestString
  52. file.close
  53. SET file = NOTHING
  54. SET fs = NOTHING
  55.     END IF
  56. %>
  57. <!--
  58. Le paramètre ENCTYPE est vital : c'est lui qui transmet les octets
  59. du fichier à copier. La méthode de postage doit être "Post".
  60. -->
  61. <FORM action="upload.asp" Method="Post" ENCTYPE="multipart/form-data">
  62.   <INPUT Type="file" Name="file"> <BR>
  63.   <INPUT Type="Submit" Value="Envoyer">
  64. </FORM>


 
(sur le net ca, car moi et l'asp.... ^^)
 

Code :
  1. <% ' Export MSAccess >> MySQL
  2. Set FSO = Server.CreateObject("Scripting.FileSystemObject" )
  3. BASE = "mabase" ' Adaptez votre chaîne de connexion !
  4. basePath = server.mapPath("/htdocs" ) & "\..\database\" & BASE & ".mdb"
  5. TABLE = "matable"
  6. SQL = "SELECT * FROM [" & TABLE & "]" ' par exemple
  7. ' nom du fichier d'export (dans rep déprotégé !!!)
  8. fichier = server.mapPath("/data" ) & "\" & BASE & "_" & TABLE & ".txt"
  9. ' Connexion à la base
  10. Set Conn = Server.CreateObject("ADODB.Connection" )
  11. Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & basePath
  12. ' Ouverture du recordset
  13. Set RS = Server.CreateObject("ADODB.Recordset" )
  14. RS.open SQL,conn,3,3
  15. ' Commentaires non lus par PhpMyAdmin
  16. MySQLstr = "#" & VbCrLf _
  17.    & "# Export Access >> MySQL" & VbCrLf _
  18.    & "# by ASP-PHP.net" & VbCrLf _
  19.    & "#" & VbCrLf _
  20.    & "# DB : " & BASE & VbCrLf _
  21.    & "# SQL : " & SQL & VbCrLf _
  22.    & "#" & VbCrLf & VbCrLf
  23. ' Destruction optionnelle de la table
  24. MySQLstr = MySQLstr & "DROP TABLE IF EXISTS `" & TABLE & "`;" & VbCrLf
  25. ' Création de la table
  26. MySQLstr = MySQLstr & "CREATE TABLE `" & TABLE & "` (" & VbCrLf
  27. ' Création des champs
  28. for each Champ in RS.fields
  29.    temp = "  `" & Champ.name & "` "
  30.    select case Champ.type
  31.       case 2 : ' Entier court
  32.          MySQLstr = MySQLstr & temp & "smallint," & VbCrLf
  33.       case 3 : ' Entier long
  34.          MySQLstr = MySQLstr & temp & "int," & VbCrLf
  35.       case 4 : ' Réel
  36.          MySQLstr = MySQLstr & temp & "float," & VbCrLf
  37.       case 5 : ' Réel long
  38.          MySQLstr = MySQLstr & temp & "double," & VbCrLf
  39.       case 6 : ' Monétaire
  40.          MySQLstr = MySQLstr & temp & "float(15,4)," & VbCrLf
  41.       case 11 : ' Booléen
  42.          MySQLstr = MySQLstr & temp & "char(1)," & VbCrLf
  43.       case 17 : ' Byte
  44.          MySQLstr = MySQLstr & temp & "tinyint," & VbCrLf
  45.       case 135 : ' Date/Time
  46.          MySQLstr = MySQLstr & temp & "datetime," & VbCrLf
  47.       case 200,202 : ' Texte
  48.          MySQLstr = MySQLstr & temp & "varchar(" & Champ.definedsize & " )," & VbCrLf
  49.       case 201,203 : ' Mémo
  50.          MySQLstr = MySQLstr & temp & "longtext," & VbCrLf
  51.       ' sinon non exporté...
  52.    end select
  53. next
  54. ' Fin de la création de la table
  55. MySQLstr = left(MySQLstr,len(MySQLstr)-3) & VbCrLf & " ) TYPE=MyISAM;" & VbCrLf & VbCrLf
  56. ' Fonctions de conversion
  57.    function n2t(n,w) ' Entier > chaine 000x ou 0x
  58.       n2t = right("000" & cStr(n),w)
  59.    end function
  60.    function txt2mys(txt) ' Caractères spéciaux pour INSERT
  61.       dim temp
  62.       if not isnull(txt) then
  63.          temp = replace(txt,VbCrLf,"\r\n" )
  64.          temp = replace(temp,chr(13),"\r\n" )
  65.          txt2mys = replace(temp,"'","\'" )
  66.       end if
  67.    end function
  68. ' Création des enregistrements
  69. while not RS.eof
  70.    MySQLstr = MySQLstr & "INSERT INTO `" & TABLE & "` ("
  71.    ' Les noms
  72.    for each Champ in RS.fields
  73.       select case Champ.type
  74.          case 2,3,4,5,6,11,17,135,200,201,202,203 : MySQLstr = MySQLstr & "`" & Champ.name & "`,"
  75.          ' sinon non exporté...
  76.       end select
  77.    next
  78.    ' Les valeurs
  79.    MySQLstr = left(MySQLstr,len(MySQLstr)-1) & " ) VALUES ("
  80.    for each Champ in RS.fields
  81.       select case Champ.type
  82.          case 2,3,4,5,6,17 : ' Numériques
  83.             MySQLstr = MySQLstr & Champ.value & ","
  84.          case 11 : ' Booléen >> char(1) : "Y" ou "N"
  85.             if(Champ.value) then MySQLstr = MySQLstr & "'Y'," _
  86.             else MySQLstr = MySQLstr & "'N',"
  87.          case 135 : ' Date time >> YYYY-MM-DD HH:MM:SS
  88.             MySQLstr = MySQLstr & "'" _
  89.             & n2t(year(Champ.value),4) & "-" _
  90.             & n2t(month(Champ.value),2) & "-" _
  91.             & n2t(day(Champ.value),2) & " " _
  92.             & n2t(hour(Champ.value),2) & ":" _
  93.             & n2t(minute(Champ.value),2) & ":" _
  94.             & n2t(second(Champ.value),2) & "',"
  95.          case 200,201,202,203 : ' Texte >> nettoyage
  96.             MySQLstr = MySQLstr & "'" & txt2mys(Champ.value) & "',"
  97.          ' sinon non exporté...
  98.       end select
  99.    next
  100.    MySQLstr = left(MySQLstr,len(MySQLstr)-1) & " );" & VbCrLf
  101.    ' Enregistrement suivant
  102.    RS.movenext
  103. wend
  104. ' Fermeture de la connexion
  105. RS.close : conn.close
  106. ' Ecriture du fichier
  107. set inF = FSO.openTextFile(Fichier,2,true)
  108. inF.writeLine(MySQLstr)
  109. inF.close
  110. ' On affiche + lien vers fichier
  111. fichier = replace(fichier,"\","/" )
  112. response.write "<A target=""_blank"" href=""" & fichier & """>" & fichier & "</A><br><br>" _
  113.    & replace(MySQLstr,VbCrLf,"<br>" )
  114. %>


 
et ca pour le sql
 
 
sinon ya plein de site ki peuvent t'aider a faire ce ke tu veut ;)


---------------
Oseras-tu m'affronter ?
n°4784
Dr Lous
I see old password
Posté le 10-01-2005 à 06:08:51  profilanswer
 

je viens de remarquer un truc,  
 
ASP <-> VB c du pareil au meme... [:rofl]


---------------
Oseras-tu m'affronter ?
n°4787
nicodache
marmotte en chocolat concept ©
Posté le 10-01-2005 à 16:03:16  profilanswer
 

Dr Lous a écrit :

je viens de remarquer un truc,  
 
ASP <-> VB c du pareil au meme... [:rofl]


je suis pas sur :o
 
tu peux aussi faire de l'asp en C# ;)
et c'est mieux je trouve, t'as pas la syntaxe pourrie du VBscript ;)
 
[edit]par contre, c'était pour ce matin, mais c'est pas grave, c'est pas trop mal passé, et le prof m'a posé quelques questions sur les css, visiblement, il connait pas ca aussi bien que moi, et je connais pas ca bien du tout :D (donc il connais vraiment pas grand choses en css :D)


Message édité par nicodache le 10-01-2005 à 16:04:17

---------------
modérateur inside [:nicodache] plankaivoo [:nicodache] - ici powered - Je roule en micra 1l 55cv et je t'emmerde :o
n°4788
Dr Lous
I see old password
Posté le 10-01-2005 à 16:20:38  profilanswer
 

ben je te l'ai mit a 6h il te faut koi de plus [:dslam]


---------------
Oseras-tu m'affronter ?
n°4790
nicodache
marmotte en chocolat concept ©
Posté le 10-01-2005 à 18:12:46  profilanswer
 

que j'y pense le matin quand je me leve, et que je sois motivé pour essayer de comprendre [:grut]


---------------
modérateur inside [:nicodache] plankaivoo [:nicodache] - ici powered - Je roule en micra 1l 55cv et je t'emmerde :o
mood
Google
Posté le 10-01-2005 à 18:12:46  profilanswer
 


Aller à :
Ajouter une réponse

  FORUM Syndrome-OC - Jacky-PC


  Programmation


  Autre


  [asp] formulaire, upload fichier, insert db

 

Hit Parade