开发者

How to Open a Remote Text File using Server.MapPath in ASP.Net?

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

Dim FIL开发者_如何转开发ENAME 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.


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","janeDoe@contoso.com");

        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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜