I am trying to upload a .docx file which is in MemoryStream
to FTP
But when upload is completed, the file is empty.
MemoryStream mms = new MemoryStream();
document2.SaveToStream(mms, Spire.Doc.FileFormat.Docx);
string ftpAddress = "example";
string username = "example";
string password = "example";
using (StreamReader stream = new StreamReader(mms))
{
// adnu is a random file name.
WebRequest request =
WebRequest.Create("ftp://" + ftpAddress + "/public_html/b/" + adnu + ".docx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
Stream reqStream = request.GetRequestStream();
reqStream.Close();
}
Write the document directly to the request stream. There's no point using an intermediate
MemoryStream
. AndStreamReader
/StreamWriter
are for working with text files, while a.docx
is a binary file format, so do not use those either.Or use
WebClient.OpenWrite
:You will only need an intermediate
MemoryStream
, if the Spire library requires a seekable stream, what theStream
returned byFtpWebRequest.GetRequestStream
is not. I cannot test that.If that's the case, use:
Or again, you can use
WebClient.OpenWrite
as in the previous example.See also a similar question Zip a directory and upload to FTP server without saving the .zip file locally in C#.