views:

2031

answers:

5

Hi,

Is it possible to upload a file directly into an ftp account folder with ASP.NET ?

E.g. I click on browse, select a file to upload and when I click "upload" button, It should save it directly to the folder on another web server located at somewhere else other then the server that is being used to upload.

A: 

EDIT

First off the @ sign is there to flag the string as a literal. it saves you having to escape characters like backslashes. e.g.

string path = "Z:\\Path\\To\\File.txt";
string path = @"Z:\Path\To\File.txt";

Secondly, if you only have FTP Access to the other server, then you can take, the FileUpload.FileBytes Property of the FileUpload control. That will give you a byte[] of the file contents.

From this you use the System.Net.FtpWebRequest & System.Net.FtpWebResponse to upload your file to the FTP Account.

Theres some sample code here in VB.NET but it should be easy enough for you to figure out

http://www.programmingforums.org/thread15954.html

ORIG

The file upload control will provide you with the File on your webserver.

It would be up to you, to copy/save that file then from the webserver to whatever server you're FTP is hosted on.

Do you have a UNC Path/Mapped Drive shared out on your other server that you can save to.

The FileUpload Control has a .SaveAs() method so it's just a simple matter of

if (FileUpload1.HasFile)
try
{
    FileUpload1.SaveAs(@"Z:\Path\On\Other\Server\" + FileUpload1.FileName);
}
Eoin Campbell
thank you for the answer.. Can you tell me what the @ sign is used for? and if I can use an IP address rather then a physical path as the web page will be on a different server than the server I will be uploading the files to.. e.g. fpt.mydomain.com/images and in that case if this can be done, I will probably need to provide user credentials right? if so how?
Emin
The @ site creates a vertabum string, basically removing the need to escape \ as \\
Richard Szalay
Or verbatim, even ;)
Richard Szalay
For more information on verbatim strings: http://msdn.microsoft.com/en-us/library/aa691090(loband).aspx
Richard Szalay
A: 

You cannot upload it to an FTP directly from your HTML form. However, you can upload it to your ASP.NET application and then upload it to the FTP from there using FtpWebRequest.

Richard Szalay
+1  A: 

As I understand your question, you want to upload the file to another remote server (so it's not another server sitting on the same network as your web server)? In that case you can do a couple of different things. The easiest way is perhaps to start by making a regular file upload you your server, and then have your server send the file via FTP to the other remote server:

string fileName = Path.Combine("<path on your server", FileUpload1.FileName);
FileUpload1.SaveAs(fileName);
using(System.Net.WebClient webClient = new System.Net.WebClient())
{
    webClient.UploadFile(
        New Uri("ftp://remoteserver/remotepath/" + FileUpload1.FileName), 
        localFile);
}

...or it might work doing it in one step:

using(System.Net.WebClient webClient = new System.Net.WebClient())
{
    webClient.UploadData(
        New Uri("ftp://remoteserver/remotepath/" + FileUpload1.FileName), 
        FileUpload1.FileBytes);
}

(I din't try this code out, so there could be some errors in it...)

Update: I noticed that I was wrong in assuming that the UploadXXX methods of WebClient were static...

Fredrik Mörk
+2  A: 

You can use the WebClient class to store the uploaded file to FTP (without saving it as a file on the server). Something like this:

string name = Path.GetFileName(UploadControl.FileName);
byte[] data = UploadControl.FileBytes;

using (WebClient client = new WebClient()) {
   client.UploadData("ftp://my.ftp.server.com/myfolder/" + name, data);
}
Guffa
A: 

hello

I want to upload a page in my existing ASP website through FTP,

I did it a lot of time but when i browse my website, it cannot show the one which i upload,

Can any body help me to upload through FTP

shah