簡體   English   中英

如何將 XML 文檔上傳到 FTP 位置

[英]How do I upload XML document to FTP location

我有一個 XML 文檔。 我不想將該 XML 保存在物理路徑中。 如何將內存中的 xml 文檔上傳到 ftp。

所以,我想知道是否有可能在內存中有一個對象並保存到 ftp。 我有上傳到ftp的代碼,它將本地路徑和遠程路徑作為參數並上傳。

UploadXMLToFTP(XmlDocument xml)

//Now this XMLDocument should be uploaded to ftp without saving in physical drive.

遵循如何在 .NET 中通過 FTP 上傳文件的MSDN 示例

using System;
using System.IO;
using System.Net;
using System.Text;

...
public static void UploadXMLToFTP (XmlDocument xml)
{
    // Get the object used to communicate with the server.
    using(FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm"))
    {
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

        // Copy the contents of the file to the request stream.
        request.ContentLength = xml.OuterXml.Length;

        Stream requestStream = request.GetRequestStream();
        xml.Save(requestStream);
        requestStream.Close();


        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM