|
Transfer Files Over the Internet
You can use FTP or HTTP POST to transfer files over the Internet. Also learn how to secure a database connection string.
by Doug Thews
Posted February 6, 2004
Technology Toolbox: VB.NET, C#, SQL Server 2000, ASP.NET
Q:
How do I transfer files to a remote server over the Internet using .NET?
A:
There are two standard ways to transmit files over the Internet. The first is to use the HTTP POST verb to transmit files (or just plain data) to a receptor, which then saves them on the remote server. The second is to use File Transfer Protocol (FTP) to connect to a remote server, then transmit the files directly to the remote FTP server, which saves them on the remote server.
HTTP posting requires two pieces. The first piece is the program that recognizes HTTP POST requests (an acceptor), gets the filenames and file data from the request, and saves them in the desired location. The second piece includes the file data's sender and the desired filename (a poster).
I'll walk you through a sample project that shows you how to do this, studying the acceptor first. Name the acceptor Uploader.aspx, and use the Page_Load event to house all your code (see Listing 1). All the acceptor does is look for the filename sent from the requestor, get the file data, and save the data to a specified location on the local server. This Web page includes a single label populated with the status of the save. The poster can query the HTML within the page and look for this label to determine the status of the operation. You could easily modify the acceptor so it saves the data to another location or saves the data dynamically based on some other data posted by the requestor. Finally, compile the project and deploy it to an ASP.NET Web server.
You need to build a poster after you deploy the acceptor. This sample poster is a small VB.NET Windows Forms app that posts the data. The poster can be either a Web page or a rich desktop client. The sample WinForms app provides textboxes for the user to enter a local filename to upload and the name of the acceptor (Uploader.aspx, in this case).
Once the proper entries are made, you use the WebClient class to establish a connection to the specified acceptor. Next, use the UploadFile method to take a file on the local machine and post it to the acceptor:
myWebClient = New WebClient
httpResponseArray = _
myWebClient.UploadFile( _
uriString, "POST", _
LocalFileName)
The UploadFile method passes back a byte array, the string of HTML returned by the Uploader.aspx file. This lets you look for the label you output as part of the acceptor so you can determine the status of the save.
Sending a file using the HTTP POST method is easy. Simply open up a WebClient connection to the desired target and use the UploadFile method to send a file using the HTTP POST command. The uriString is the complete URI to the acceptor that you deployed (for example: http://www.myserver.com/TestUploader/Uploader.aspx). Note that you must specify the complete URI and the filename. The UploadFile method won't work if you use only the directory and set Uploader.aspx to be the default page for that directory. The page name must be part of the URI passed to UploadFile.
Back to top
|