Uploader vers mon serveur distant

ericantoineeric Messages postés 4 Date d'inscription dimanche 19 avril 2009 Statut Membre Dernière intervention 8 septembre 2009 - 25 août 2009 à 23:23
PascalCmoa Messages postés 239 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 17 janvier 2013 - 1 sept. 2009 à 15:35
Je suis débutant en aspx (vbnet) et je ne parviens pas à comprendre le fonctionnement de l'UPLOADING .
Si quelqu'un pouvait me donner un petit script (vnet) en exemple et quelques explications (afin de devenir un peu moins con), je pourrais peut être enfin uploader des fichier wmv, jpg ou gif de petites tailles sur mon site...

7 réponses

ggtry Messages postés 417 Date d'inscription vendredi 13 juin 2008 Statut Membre Dernière intervention 21 juillet 2010 1
27 août 2009 à 02:01
Bonsoir,

Tu peux écrire, en asp :




Cela va te créer un premier bouton, pour que l'utilisateur puisse sélectionner les fichiers sur son disque. Une fois son choix fait, il devra cliquer sur le second bouton, pour envoyer les fichiers.
Un code possible sera alors (un Label, nommé ici Labelc1 sert à afficher un éventuel problème ; dans cet exemple, on ne peut télécharger que des jpg ou des gif) :

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Labelc1.Text = ""
Dim path As String = Server.MapPath(*** ton chemin sur ton serveur ***)
Dim fileOK As Boolean = False
If FileUpload1.HasFile Then
Dim fileExtension As String
fileExtension = System.IO.Path.GetExtensio(FileUpload1.FileName).ToLower()
Dim allowedExtensions As String() = {".jpg", ".gif"}
For i As Integer = 0 To allowedExtensions.Length - 1
If fileExtension = allowedExtensions(i) Then
fileOK = True
End If
Next
If fileOK Then
Try
FileUpload1.PostedFile.SaveAs(path & FileUpload1.FileName)
Labelc1.Text = "Le fichier a été chargé."
Catch ex As Exception
Labelc1.Text = "Problème ! Le fichier n'a pas pu être chargé."
End Try
Else
Labelc1.Text = "Les extensions doivent être de type .jpg ou .gif"
End If
Else
Labelc1.Text = "Pas de fichier sélectionné"
End If
End Sub

C'est un exemple classique, légérement adapté.
J'espère que ça ira...

GGtry
0
ericantoineeric Messages postés 4 Date d'inscription dimanche 19 avril 2009 Statut Membre Dernière intervention 8 septembre 2009
30 août 2009 à 05:10
Toujours en tant que débutant, je te remercie GGtry pour ta réponse même si je n'ai toujours pas réussi à UPLOADER;
Lorsque que je clique sur ENVOYER (Button1): une page d'erreur s'affiche, problème de chemin?, d'autorisation?, ou j'ai toujours pas compris la méthode MapPath?...
voici le script de ma page (upl.aspx):

<%@ Page Explicit= "True" Language="VB" Debug="True" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Upl.aspx</title>
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Labelc1.Text = ""
Dim path As String = Server.MapPath(" http://www.monsiteweb.com/www/images/ ")
Dim fileOK As Boolean = False
If FileUpload1.HasFile Then
Dim fileExtension As String
fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower()
Dim allowedExtensions As String() = {".jpg", ".gif"}
For i As Integer = 0 To allowedExtensions.Length - 1
If fileExtension = allowedExtensions(i) Then
fileOK = True
End If
Next
If fileOK Then
Try
FileUpload1.PostedFile.SaveAs(path & FileUpload1.FileName)
Labelc1.Text = "Le fichier a été chargé."
Catch ex As Exception
Labelc1.Text = "Problème ! Le fichier n'a pas pu être chargé."
End Try
Else
Labelc1.Text = "Les extensions doivent être de type .jpg ou .gif"
End If
Else
Labelc1.Text = "Pas de fichier sélectionné"
End If
End Sub
</script>
</head>

<form runat="server">



</form>

</html>
0
ggtry Messages postés 417 Date d'inscription vendredi 13 juin 2008 Statut Membre Dernière intervention 21 juillet 2010 1
30 août 2009 à 12:30
Bonjour,

Essaie en écrivant :
Dim path As String = Server.MapPath("images/")

(si ton dossier 'images' se trouve dans le même répertoire que la page à partir de laquelle tu fais l'upload ; sinon, à toi d'écrire le chemin correct : mais tu ne dois pas utiliser l'adresse de ton site).

GGtry
0
ericantoineeric Messages postés 4 Date d'inscription dimanche 19 avril 2009 Statut Membre Dernière intervention 8 septembre 2009
30 août 2009 à 14:01
Dim path As String = Server.MapPath(" images/ ")
Ca n'upload toujours pas

Ma page 'upl.aspx' fonctionne correctement à l'exception du fait que: 'Problème ! Le fichier n'a pas pu être chargé.'

Pourtant:
chemin de ma page d'upload = ftp://ftp.monsiteweb.com/www/upl.aspx
chemin de mon dossier IMAGES = ftp://ftp.monsiteweb.com/www/images/
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
ggtry Messages postés 417 Date d'inscription vendredi 13 juin 2008 Statut Membre Dernière intervention 21 juillet 2010 1
31 août 2009 à 00:16
Bonsoir,

Je n'ai aucune idée de ce qui pose problème.
A la place du "Try ... End Try" écrit simplement :

FileUpload1.PostedFile.SaveAs(path & FileUpload1.FileName)

et rapporte moi le message d'erreur.

GGtry
0
ericantoineeric Messages postés 4 Date d'inscription dimanche 19 avril 2009 Statut Membre Dernière intervention 8 septembre 2009
31 août 2009 à 12:39
Bonjour GGtry

Voici le message d'erreur:

Source Error:


Line 27: If fileOK Then
Line 28:
Line 29: FileUpload1.PostedFile.SaveAs(path & FileUpload1.FileName)
Line 30:
Line 31: Else


Source File: d:\www\monsiteweb\www\upl.aspx Line: 29

Stack Trace:


[UnauthorizedAccessException: Access to the path 'd:\www\monsiteweb\www\images\final.gif' is denied.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +651
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +1038
System.IO.FileStream..ctor(String path, FileMode mode) +64
System.Web.HttpPostedFile.SaveAs(String filename) +87
ASP.upl_aspx.Button1_Click(Object sender, EventArgs e) in d:\www\monsiteweb\www\upl.aspx:29
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
-------------------------------------------------------------
Je ne comprend pas le'd:\www\monsiteweb\www\upl.aspx'
0
PascalCmoa Messages postés 239 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 17 janvier 2013 5
1 sept. 2009 à 15:35
Juste un question, as-tu les droits pour uploader un fichier dans le répertoire ?


PascalCmoa
email: PascalCmoa
0
Rejoignez-nous