2

I want to create an Excel file on FTP server. I've tried creating a file locally and it works so to the problem: How do I create it in FTP instead of local drive?

ExcelPkg.SaveAs(
  new FileInfo(@"C:\ExcelTest\" + DateTime.Now.ToString("yyyy-MM-dd--hh-mm-ss") + ".xlsx"))

And I want to create this file directly in FTP and NOT locally and then move it.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
leiit
  • 97
  • 1
  • 8
  • 1
    What problem? You don't say what you tried, what happened and what did not work. – Equalsk Jun 25 '18 at 13:38
  • Problem is how do I create my excel file in FTP server, how do I set the right path. – leiit Jun 25 '18 at 13:51
  • `ExcelPkg` is a excel libary in C# and it's called EpPlus – leiit Jun 25 '18 at 13:52
  • Again, you don't state what you tried, what happened and what did not work. If you put `ExcelPkg.SaveAs(new FileInfo(YourFtpPath))` what happens? – Equalsk Jun 25 '18 at 13:56
  • Well NOTHING happens at all, code compile with 0 ERRORS and nothing shows up at the server – leiit Jun 25 '18 at 13:58
  • Can you give an example of your FTP path? Obviously not the real thing. – Equalsk Jun 25 '18 at 14:02
  • `ftps://_username_:_password_@IP:PORT/_directory_/` like that looks my FTPs – leiit Jun 25 '18 at 14:09
  • @MartinPrikryl the code for creating a file is complete if that is what you mean, only that I need a way to create it on the right path, so is there any way to access FTPs and use its path – leiit Jun 25 '18 at 14:20
  • You'll probably be **MUCH** better off creating the file locally, and then uploading to the FTP server as a separate operation. FTP was not designed with this kind of file interaction in mind. – Joel Coehoorn Jun 25 '18 at 14:35
  • @JoelCoehoorn Well I have now walked in a dead end, how would your solution look like? if you could post an answer – leiit Jun 26 '18 at 07:25

2 Answers2

2

Use ExcelPackage.SaveAs overload that accepts a Stream and pass it an FTP request stream, as for example shown in:
Upload a streamable in-memory document (.docx) to FTP with C#?


I cannot test EPPlus right now, but this should work:

WebRequest request = WebRequest.Create("ftp://ftp.example.com/remote/path/sheet.xlsx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
    ExcelPkg.SaveAs(ftpStream);
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • This should work... but it's probably better if the OP just creates the file locally and then uploads it as a separate operation. – Joel Coehoorn Jun 25 '18 at 14:33
  • Thank you for the solution, I think It is in the right direction but I now get an error says "~Can not connect to the server" and that depends maybe on Encryption : (Encryption: Require implicit FTP over TLS) Know anything about that? – leiit Jun 25 '18 at 15:08
  • Try `request.EnableSsl = true;`. If that does not help, because your server really requires implicit TLS/SSL (what is very very rare), you will have to use a completely different solution - https://stackoverflow.com/q/1842186/850848 – Martin Prikryl Jun 25 '18 at 17:35
  • 1
    @JoelCoehoorn Why? It makes no sense to create a file locally, if you have an API that can do without that. By relying on a temporary file, you are just adding yet another unnecessary point of failure. – Martin Prikryl Jun 25 '18 at 17:36
2

I had to make it 2 different operations first one creates the Excel file and then this, uploads existing file and remove it afterward. Using a library called WinSCP I couldn't find any library that supports 90s Encryptions like Implicit FTP over TLS and streaming API at the same time. So there was no way for me to directly create the excel file on the server but to temporary create it locally to copy it over and remove it at the same time, it is almost the same thing I get the same result. And here's solution for doing it

try
{
    // Setup session options
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Ftp,
        HostName = @"xx.xx.x.x",
        UserName = "xxxx",
        Password = "xxxxxx",
        PortNumber = xxxx,
        FtpSecure = FtpSecure.Implicit, //Encryption protocol
        GiveUpSecurityAndAcceptAnyTlsHostCertificate = true // Accepts any certificate
    };

    using (Session session = new Session())
    {
        // Connect
        session.Open(sessionOptions);

        // Upload files
        TransferOptions transferOptions = new TransferOptions();
        transferOptions.TransferMode = TransferMode.Binary;

        TransferOperationResult transferResult;
        // Copy's Existing file to Connected server and delets the old one.
        // change by replace "true" with "false".
        transferResult =
            session.PutFiles(
                ExistingPath, "/ScheduleJobs/" + RemotePath, true, transferOptions);

        // Throw on any error
        transferResult.Check();

        // Print results
        foreach (TransferEventArgs transfer in transferResult.Transfers)
        {
            Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
            Console.ReadLine();
        }

    }

}
catch (Exception e)
{
    Console.WriteLine("Error: {0}", e);
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
leiit
  • 97
  • 1
  • 8
  • Would you include an explanation, why you needed to use this solution? – Martin Prikryl Jun 26 '18 at 11:51
  • because I couldn't find any library that supports advanced Encryptions like `Implicit FTP over TLS ` so there was no way for me to directly create the excel file on the server but to temporary create it locally to copy it over and remove it, it is almost the same thing I get the same result, but the way I wanted would be "much" better to just create it on server. And here's solution for doing it. – leiit Jun 26 '18 at 12:24
  • @leit I know all that, but your answer (not comment) should explain that you had to use this suboptimal solution, only because limitations of your server - *"Implicit FTP over TLS"* is not any *"advanced encryption"* - It's actually a legacy hack from 90s, that no modern FTP server needs to use. – Martin Prikryl Jun 26 '18 at 13:34