The user selects a file from the local disk and the page manages to persist it to a server location : FileUpload « ASP.net Controls « ASP.NET Tutorial






<%@ 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>








3.19.FileUpload
3.19.1.Accepting File Uploads
3.19.2.FileUpload Test
3.19.3.Upload file to server
3.19.4.Uploading files using the new FileUpload control (C#)
3.19.5.Uploading files using the new FileUpload control (VB)
3.19.6.Changing the file-size limitation setting in the web.config file
3.19.7.The user selects a file from the local disk and the page manages to persist it to a server location