Ali_dotNet
Ali_dotNet

Reputation: 3279

copy file from local to remote server

I'm dynamically creating html files on my local system (using HTMLTEXTWRITER, then save them using streamwriter to local file system). I want to copy this file to my remote server withour user interaction, so that my users can read file. I use C#

for instance I want to copy from d:\myfile.html to mysite.com\myfile.html, how can I do it?

Upvotes: 3

Views: 9377

Answers (5)

snaidua
snaidua

Reputation: 36

I have used this and it worked. may be useful

for holding path of local

rPath = "\\" & Request.UserHostAddress & "\c$\temp\"

for output file

rOutput = Session.SessionID & "_" & Format(Date.Now(), "ddMMyyhhmmss") & ".pdf"

now: report will be created at localhost\c\temp

Upvotes: 2

Ramesh
Ramesh

Reputation: 13266

There are three ways by which you can copy the file to remote server.

  1. Using normal file copy mode. Here you need to have access to the the webserver shared path. If the webserver is in same network as your application, then you can share the webroot and provide write access to the user who is running the application. He can then use File.Copy("source.txt", "\\Servername\SharedFolderName\target.txt").

  2. The second approach is to use FTP to copy the file to the remote server. This MSDN example would help you on how to do this. This will work with most of the shared hosting providers.

  3. You can use HTTP POST as noted by Tim. But this would let any user to perform the post. You may have to take care of user provisioning, authentication and authorization. IMO, keep this as last option as provisioning user and providing rights to certain path, may become cumbersome.

Upvotes: 1

misha
misha

Reputation: 2889

You could set-up an FTP server and copy the files programmatically via FTP. An example would be found here or here.

Upvotes: 1

Johnny Qian
Johnny Qian

Reputation: 713

Is you remote server based on Windows and in the same workgroup or domain with you working machine ? If so, you can turn on the Windows File Sharing on the server. Then you can copy your file with cmd like this:

copy c:\test.txt \\mysite.com

The path "\\mysite.com" is also valid used by File.Copy in C#.

Otherwise, you need to set up a FTP environment on you server and use the FTP related API in C#.

Upvotes: 1

Tim M.
Tim M.

Reputation: 54377

You can't use the System.IO classes for this (unless you have access to the remote server as a network drive), but you can programmatically POST the file from the client to the remote server over HTTP using System.Net.

Here's a snippet using the WebRequest class:

WebRequest request = WebRequest.Create( url );
request.Timeout = 1000; // some appropriate value
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = 0; // set a length here

using( StreamWriter requestStream = new StreamWriter( request.GetRequestStream(), System.Text.Encoding.UTF8) ) {
    // write to the stream here using requestStream.Write();
    requestStream.Close();
}

More info for HTTP: http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

Alternatively, you could use a protocol designed for transferring files like FTP (or something more secure) which isn't that hard to do in code.

FTP options: http://msdn.microsoft.com/en-us/library/ms229718

Upvotes: 1

Related Questions