upload to ftp asp.net

2019-02-07 08:43发布

问题:

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.

回答1:

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...



回答2:

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);
}


回答3:

    /// <summary>
    /// Example call : if (FtpUpload(FileUploadControl1, "ftp.my.com/somePathDir", @"user", "pass!", "domain"))
    /// </summary>
    /// <param name="file"></param>
    /// <param name="ftpServer"></param>
    /// <param name="username"></param>
    /// <param name="ftpPass"></param>
    /// <returns></returns>
    private bool FtpUpload(FileUpload file, string ftpServer, string username, string ftpPass, string domainName = "")
    {
        // ftp://domain\user:password@ftpserver/url-path
        // If you are a member of a domain, then "ftp://domain-name\username:password@url-path" may fail because the backslash (\) is sent in as a literal character and Internet Explorer incorrectly looks for a file instead of parsing a Web address. Changing the backslash (\) in the domain-name\username to domainname%5Cusername works correctly.

        try
        {
            string ftpAddres;
            if (domainName != string.Empty)
                ftpAddres = "ftp://" + domainName + @"%5C" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName;
            else
                ftpAddres = "ftp://" + username + ":" + ftpPass + "@" + ftpServer + "/" + file.FileName;

            using (var webClient = new System.Net.WebClient())
            {
                webClient.UploadData(new Uri(ftpAddres), file.FileBytes);
            }

        }
        catch (Exception e)
        {
            throw new Exception(e.Message, e);
        }
        return true;
    }


回答4:

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.



回答5:

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);
}