Zayd Chopdat
Zayd Chopdat

Reputation: 87

Creating PDF on FTP server in C#

I'm trying to create a PDF on my host server.

I understand that I must use FTP authentication, however I'm not sure how to achieve writing the iTextSharp and FileStream.

FileStream fs = new FileStream(Server.MapPath("Quotation/quote.pdf"), 
    FileMode.Create, FileAccess.ReadWrite, FileShare.None);
string imagepath = "images/logo/logo4.png";
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();

So I need to authenticate that creation to the FTP server.

Upvotes: 0

Views: 1003

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202330

You cannot use FileStream with FTP.

Use FtpWebRequest and its FtpWebRequest.GetRequestStream to upload an in-memory generated content to FTP server:

FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.pdf");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;  

using (Stream ftpStream = request.GetRequestStream())
{
    Document doc = new Document();
    PdfWriter writer = PdfWriter.GetInstance(doc, ftpStream);
    doc.Open();
    // ...
}

Upvotes: 2

Related Questions