Home DotNet Easily Upload file with ASP.NET 1.1
Author:

With asp.net 2.0 you can use the FileUpload control to upload file, however if you have to work with ASP.NET 1.1 then the FileUpload control is not available. So you will need to use the classic input tag.

Here is an example.

ASPX PAGE:

<
table width="964" border="0" cellpadding="0" cellspacing="0">

    <tr>

        <td width="140" height="200" valign="top"></td>

        <td width="10">&nbsp;</td>

        <td width="774" valign="top">

            <asp:Panel id="pnlImport" runat="server">

                <DIV class="displayFrame" id="uploadFrame">

                    <P>Select file to upload: <INPUT id="fileUploader" type="file" runat="server">

                        <asp:Button id="btnUpload" onclick="btnUpload_Click" runat="server" Text="Upload"></asp:Button></P>

                    <P>

                        <asp:Label id="lblMessage" runat="server" CssClass="alert" Visible="false"></asp:Label></P>

                </DIV>

            </asp:Panel>

        </td>

    </tr>

</table>

 

CODE PAGE:

try

{

                //Ensure that the user has selected a file to upload.

                if ((colFiles.Count == 1 & colFiles(0).ContentLength > 0))

                {

                    System.Web.HttpPostedFile objCurrFile = colFiles(0);

                    strCurrFileName = System.IO.Path.GetFileName(objCurrFile.FileName);

 

                    strCurrFileExt = util.GetFileExtension(strCurrFileName);

                    //Ensure that this is a valid file name.

                    //Change this code if you are upload a different file other then an Excel file.

                    if (!string.IsNullOrEmpty(strCurrFileName) & strCurrFileExt == "xls")

                    {

                        filePath = Server.MapPath("~\\Uploads\\") + strCurrFileName;

                        objCurrFile.SaveAs(filePath);

                    }

                    else

                    {

                        lblMessage.Text = "The selected file is invalid. Please select an Excel to upload.";

 

                    }

 

                }

            }

            catch (Exception ex)

            {

                lblMessage.Text = "An error has occured. " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace;

            } 

}



Comments (0)
Write comment
Your Contact Details:
Comment:
Security
Please input the anti-spam code that you can read in the image.

"