Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

c# - Upload data from memory to SFTP server using SSH.NET

I am hoping to write PDF's directly to SFTP site. The PDF's are generated from ReportExecutionService.Render (SSRS).

ReportExecutionService rsExec = new ReportExecutionService();

I have been able to write the files locally using FileStream. I am generating millions of files, so I am hoping to write them directly on SFTP using SSH.NET. What is the syntax for taking the rsExec.Render result and writing to SFTP using SSH.NET?

string deviceInfo = null;
string extension;
string encoding;
string mimeType;
ConsoleApp2.ReportExecution2005.Warning[] warnings = null;
string[] streamIDs = null;
string format = "PDF";

Byte[] results =
    rsExec.Render(
        format, deviceInfo, out extension, out mimeType, out encoding,
        out warnings, out streamIDs);
FileStream stream = File.OpenWrite("C:\test.pdf");
stream.Write(results, 0, results.Length);
stream.Close();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Copy the byte array to a MemoryStream and use SftpClient.UploadFile to upload it

var client = new SftpClient(host, port, username, password);
client.Connect();
var stream = new MemoryStream();
stream.Write(results, 0, results.Length);
stream.Position = 0;
client.UploadFile(stream, "/remote/path/my.pdf");

Though most libraries can use the Stream API directly. That would be more efficient to use, instead of copying the data over via byte[] array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...