How to Open a Remote Text File using Server.MapPath in ASP.Net?
-
21-09-2019 - |
Question
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.
Solution
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow