Upload file through ASP.net

<% @Page Language="C#" %>
<html>
<head>
  <title>File upload in ASP.NET</title>
</head>
<body bgcolor="#ffffff" style="font:8pt verdana;">
<script language="C#" runat="server">
void btnUploadTheFile_Click(object Source, EventArgs evArgs)
{
  string strFileNameOnServer = txtServername.Value;
  string strBaseLocation =

"c:\\Inetpub\\vhosts\\domain.com\\httpdocs\\test2\\";
 
  if ("" == strFileNameOnServer)
  {
    txtOutput.InnerHtml = "Error - a file name must be specified.";
    return;
  }

  if (null != uplTheFile.PostedFile)
  {
    try
    {
      

uplTheFile.PostedFile.SaveAs(strBaseLocation+strFileNameOnServer);
      txtOutput.InnerHtml = "File <b>" +
        strBaseLocation+strFileNameOnServer+"</b> uploaded

successfully";
    }
    catch (Exception e)
    {
      txtOutput.InnerHtml = "Error saving <b>" +
        strBaseLocation+strFileNameOnServer+"</b><br>"+ e.ToString();
    }
  }
}
</script>

<table>
<form enctype="multipart/form-data" runat="server">
<tr>
  <td>Select file:</td>
  <td><input id="uplTheFile" type=file runat="server"></td>
</tr>
<tr>
  <td>Name on server:</td>
  <td><input id="txtServername" type="text" runat="server"></td>
</tr>
<tr>
  <td colspan="2">
  <input type=button id="btnUploadTheFile" value="Upload"
                    OnServerClick="btnUploadTheFile_Click"

runat="server">
  </td>
</tr>
</form>
</table>
    
<span id=txtOutput style="font: 8pt verdana;" runat="server" />

</body>
</html>
  • 12 Users Found This Useful
Was this answer helpful?

Related Articles

Sending email using CDOSYS

If you are using a Windows 2000 / 2003 Server, or even XP Pro chances are that CDOSYS is your...

Mailenable Component - Mail Code

Mailenable Component - Mail Code <%@ Language=VBScript %> <% Dim oMail Set oMail =...

How to create MSAccess Database DSN in Plesk Control Panel?

To Create DSN for MSAccess Database do following steps. 1. Access your control panel 2. Click on...

Create MS Access DSNLess connection string

Many people have requested this so here it is: "DRIVER={Microsoft Access Driver (*.mdb)};...

MS SQL Server 2005 connection String

MS SQL server 2005 & 2008 connection string : ------------------------------------------...