[c#][ASP.net 2.0]Problème envois de fichiers dans un dossier.
Bonjour,
Je voudrais uploader des fichiers depuis une page asp.net. Après quelque recherche, j'ai trouvé ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| if (FileUpload1.HasFile)
try
{
string strPath = "c:/tmp/"+DropDownList1.SelectedItem+"/";
DirectoryInfo di = new DirectoryInfo(strPath);
if (di.Exists)
{
FileUpload1.SaveAs(strPath);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
Label1.Text = "Fichier correctement enregistré sous <b>" + strPath + "</b>.";
}
else
{
DirectoryInfo repertoire = Directory.CreateDirectory(strPath);
FileUpload1.SaveAs(strPath);
Label1.Text = "Création du repertoire "+DropDownList1.SelectedItem+" et enregistrement sous <b>" + strPath + "</b>.";
}
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}
} |
Le chemin est construit en fonction de certains paramètres, l'utilisateur choisie une catégorie (ex : internet) à partir d'une liste (c'est la drowdownlist1). Ce code ne fonctionne pas, j'ai toujours le message "impossible d'accéder au répertoire". La création du répertoire ne fonctionne pas et même quand il existe, un message d'erreur m'indique qu'il est inaccéssible. J'ai pourtant crée un repertoire accéssible en écriture.
Voici le code de ma page aspx :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <table width="920px">
<tr>
<td style="width: 246px">
<asp:FileUpload ID="FileUpload1" runat="server" /></td>
<td colspan="2">
</td>
</tr>
<tr>
<td style="width: 250px" valign="top">
Type :<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="dstypefic" DataTextField="libel_type" DataValueField="idtype">
</asp:DropDownList><asp:SqlDataSource ID="dstypefic" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString31 %>"
SelectCommand="SELECT * FROM [typefic]"></asp:SqlDataSource>
</td>
<td style="width: 340px" valign="top">
Nom :
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td valign="top">
Détails :<br />
<textarea id="TextArea1" style="width: 303px; height: 75px"></textarea></td>
</tr>
<tr>
<td colspan="3">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Upload File" />
<asp:Label ID="Label1" runat="server"></asp:Label></td>
</tr>
</table> |
Merci... d'avance.