Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Application Uploading File

Status
Not open for further replies.

Dannybe2

Programmer
Jan 31, 2001
136
GB
Hi,

I am trying to create a simple C# application that I can run from my home computer that will upload a file to some web space I have.

I have managed to create an ASP.NET file that I have uploaded and when I browse for a file and upload it, it works fine.

My question is how to do this from a C# application as opposed to a web one? I have looked into using Client.UploadFile, but this does not seem feasible.

This is an example of the ASP.NET code I am using:

Code:
<% @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:\\temp\\";
  
  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>

I now need to use this from my computer to upload the file. Any suggestion of how to go about this would be very helpful...

Thanks,
Dan
 
i made a very crude one a while back where the file is just posted to a web page using the browser control, which ghen saves the file to disk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top