0

Currently I am generating an xml file for download using posted fields with the following code:

    string attachment = "attachment; filename=" + FileName + ".xml";
    Response.ClearContent();
    Response.ContentType = "application/xml";
    Response.AddHeader("content-disposition", attachment);
    Response.Write(Session["FileForDownload"]);
    Response.End();

This is working fine.

However I want to sftp upload the generated file to a specified directory on a server.

I have had success in connecting using ssh.net and have been able to create a new directory etc.

My question is how can I generate the file and then sftp it using ssh.net?

I've tried using a file stream with no success. I'm guessing the file needs to be temporarily stored and then retrieved for upload.

This is my current code segment for the specified problem:

        SftpClient sftp = new SftpClient("host", "user", "pwd");
        sftp.Connect();
        sftp.ChangeDirectory("directory/");
        Stream fs = File.OpenRead(Server.MapPath(@"filetobeuploaded"));
        sftp.UploadFile(fs, Session["FileName"].ToString());
        sftp.Disconnect();

I recognize that there won't be a file already on the server to upload.

Any help would be much appreciated as this is the final piece of the puzzle in my application.

Cheers

2
  • Just had a thought. I could create a temp file on the server, upload and then delete. This cannot be the most efficient way surely? Commented Nov 15, 2013 at 12:20
  • How is your operation being carried out? i mean do you want to generate xml and immediately upload it on SFTP server? or you generate XML some where and upload it from somewhere else in your code? Commented Nov 29, 2013 at 11:50

1 Answer 1

0

Fixed: I found a solution by generating a temp XML file in the server, uploading and deleting it. Thanks for your reply anyway

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.