2017-11-26 14 views
0

MemoryStreamにある.docxファイルをアップロードしようとしていますストリーミング可能なメモリ内のドキュメント(.docx)をC#でFTPにアップロードしますか?

アップロードが完了したら、ファイルは空です。

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(); 
} 
+1

あなたはリクエストストリームに書き込みをしていませんか? –

+0

[MSDNの例]に従ってください(https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-upload-files-with-ftp) – Filburt

+0

私は試みましたが、そうではありません私のために働く。結果が壊れているか空白のファイル –

答えて

0

ドキュメントを要求ストリームに直接書き込んでください。中間点MemoryStreamを使用している点はありません。 StreamReader/StreamWriterはテキストファイルを扱うためのものであり、.docxはバイナリファイル形式なので、それらも使用しないでください。

WebRequest request = WebRequest.Create("ftp://ftp.example.com/remote/path/document.docx"); 
request.Method = WebRequestMethods.Ftp.UploadFile; 
request.Credentials = new NetworkCredential(username, password); 
using (Stream ftpStream = request.GetRequestStream()) 
{ 
    document2.SaveToStream(ftpStream, Spire.Doc.FileFormat.Docx); 
} 

あなただけのスパイアライブラリがシーク可能なストリームを必要とする場合、どのようなStreamFtpWebRequest.GetRequestStreamによって返されたことはないが、中間MemoryStreamが必要になります。私はそれをテストすることはできません。

そのような場合、使用:

MemoryStream memoryStream = new MemoryStream(); 
document2.SaveToStream(memoryStream, Spire.Doc.FileFormat.Docx); 

memoryStream.Seek(0, SeekOrigin.Begin); 

WebRequest request = WebRequest.Create("ftp://ftp.example.com/remote/path/document.docx"); 
request.Method = WebRequestMethods.Ftp.UploadFile; 
request.Credentials = new NetworkCredential(username, password); 
using (Stream ftpStream = request.GetRequestStream()) 
{ 
    memoryStream.CopyTo(ftpStream); 
} 

は、同様の質問Zip a directory and upload to FTP server without saving the .zip file locally in C#を参照してください。

関連する問題