如何在 ASP.Net 中使用 Server.MapPath 打开远程文本文件?

发布于 2024-08-25 23:52:10 字数 529 浏览 4 评论 0原文

这就是我现在拥有的驻留在同一服务器上的文件,并且它可以工作。

Dim FILENAME as String = Server.MapPath("Output.txt")

Dim objStreamWriter as StreamWriter
objStreamWriter = File.CreateText(FILENAME)

dr = myCommand.ExecuteReader()
While dr.Read()

objStreamWriter.WriteLine("{0}|{1}|{2:yyyy-MM-dd}|{3:yyyy-MM-dd}", dr(0),  dr(1), dr(2), dr(3))

End While
objStreamWriter.Close()

我计划完成后将文件通过 FTP 传输到另一台服务器,但有没有办法在远程服务器上执行此操作?即在远程服务器上创建一个文本文件并写入它?没有找到任何该用法的实例。在这种情况下如何使用 ServerXMLHTTP 替换 Server.MapPath ?

非常感谢您抽出时间。

This is what I have right now for a file residing on the same server and it works.

Dim FILENAME as String = Server.MapPath("Output.txt")

Dim objStreamWriter as StreamWriter
objStreamWriter = File.CreateText(FILENAME)

dr = myCommand.ExecuteReader()
While dr.Read()

objStreamWriter.WriteLine("{0}|{1}|{2:yyyy-MM-dd}|{3:yyyy-MM-dd}", dr(0),  dr(1), dr(2), dr(3))

End While
objStreamWriter.Close()

I was planning on FTPing the file to another server once done but is there a way to do it on a remote server? i.e create a text file on a remote server and write to it? Did not find any instances of that usage. How to use ServerXMLHTTP to replace Server.MapPath in this instance?

Thank you so much for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情妓 2024-09-01 23:52:10

以这种方式使用 MapPathStreamWriter 是行不通的。这适用于本地文件场景。您可以使用 FtpWebRequest 直接从 .net FTP 文件。

下面是使用 FtpWebRequest 写入文件的示例(示例来自此处 ):

        FtpWebRequest request =
            (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/output.txt");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = 
            new NetworkCredential ("anonymous","[email protected]");

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(stuff); // write your stuff here
        requestStream.Close();

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

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

        response.Close();

Using MapPath and StreamWriter this way won't work. This is meant for a local file scenario. You can directly FTP files from .net using FtpWebRequest.

Here's an example of using FtpWebRequest to write a file (Example from here):

        FtpWebRequest request =
            (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/output.txt");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = 
            new NetworkCredential ("anonymous","[email protected]");

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(stuff); // write your stuff here
        requestStream.Close();

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

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

        response.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文