Mega Code Archive

 
Categories / ASP.Net Tutorial / ASP Net Controls
 

The user selects a file from the local disk and the page manages to persist it to a server location

<%@ Page language="C#"%> <%@ Import Namespace="System.IO" %> <script runat="server">     void UploadButton_Click(object sender, EventArgs e)     {         string savePath = UploadPath.Text.ToLower();         if (!Directory.Exists(savePath))         {             Response.Write(String.Format("<h1>The upload path doesn't exist: {0}</h1>",                 savePath));             Response.End();         }         if (FileUpload1.HasFile)         {             string fileName = FileUpload1.FileName;             savePath += fileName;             FileUpload1.SaveAs(savePath);             UploadStatusLabel.Text = "File saved as: <i>" + savePath + "</i>";         }         else         {             UploadStatusLabel.Text = "You did not specify a file to upload.";         }     } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title>File Upload</title> </head> <body>     <div id="pageContent">         <form id="Form1" runat="server">           <h4>Select a picture to upload:</h4>             <b>Upload Path</b><br />             <asp:textbox id="UploadPath" runat="server" text="c:\temp\pictures\" />             <hr />             <b>Picture to upload</b><br />             <asp:fileupload id="FileUpload1" runat="server" />             <br /><br />                        <asp:button id="UploadButton"                 text="Upload"                onclick="UploadButton_Click"                runat="server">            </asp:button>                          <hr />                        <asp:label id="UploadStatusLabel" runat="server" />          </form>     </div> </body> </html>