Download to ftp asp.net

Is it possible to upload a file directly to the ftp account folder using ASP.NET?

eg. I click on the preview, select the file to download, and when I click on the “Download” button, it should save it directly in a folder on another web server located somewhere else except the server that is used for downloading.

+5
source share
5 answers

As far as I understand your question, do you want to upload the file to another remote server (so this is not another server that is on the same network as your web server)? In this case, you can do a couple of different things. The easiest way is to start by loading your server with a regular file, and then your server will send the file via FTP to another 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 he can do 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 am not trying to execute this code, so there might be some errors in it ...)

Update: I noticed that I was mistaken in believing that the UploadXXX WebClient methods were static ...

+4
source

You can use the class WebClientto store the downloaded file on FTP (without saving it as a file on the server). Something like that:

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);
}
+4
source
    /// <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

@ . , . .

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

-, FTP- , FileUpload.FileBytes FileUpload. byte[] .

System.Net.FtpWebRequest System.Net.FtpWebResponse FTP.

, VB.NET, ,

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

ORIG

-.

, / - FTP.

UNC/Mapped Drive , .

FileUpload .SaveAs(),

if (FileUpload1.HasFile)
try
{
    FileUpload1.SaveAs(@"Z:\Path\On\Other\Server\" + FileUpload1.FileName);
}
0
source

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

0
source

All Articles