asp.net, c# reading csv file

R8VI
R8VI used Ask the Experts™
on
HI,

I have the code below which reads a csv file this is no problem.
My problem starts becasue i want to get the CSV file from a different server

Please help

Thanks,

R8VI
System.Configuration.AppSettingsReader settingsReader = new System.Configuration.AppSettingsReader();
            // Get the key from config file
            string key = (string)settingsReader.GetValue("T", typeof(String));
          
            string filename = "T";
            string output;

            using (var reader = new System.IO.StreamReader(Server.MapPath("../O/T.csv")))
            {
                output = reader.ReadToEnd();
            }

            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.ContentType = "text/csv";
            Response.AppendHeader("Content-disposition", "attachment; filename=" + filename + ".csv");
            Response.AppendHeader("Content-Length", output.Length.ToString());
            Response.Write(output);
            Response.End();

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Carl TawnSenior Systems and Integration Developer

Commented:
Different server on your own domain? Different server across a VPN? Different web server? File directly available from a HTTP request, or through an API/Service?

We need a little more information, "another server" could me pretty much anything.

Author

Commented:
Sorry bit more info

CSV file gets published on database server
so need to access it from the webserver

Thanks ,

R8VI
Carl TawnSenior Systems and Integration Developer

Commented:
Is it published to a shared folder? Are you able to setup a Virtual Directory that maps to the folder on the SQL Server?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

Commented:
yeh i can make the folder a shared one
Carl TawnSenior Systems and Integration Developer

Commented:
In that case just map to the shared folder and then either create a Virtual Directory that points to the mapped folder, which will allow you to use a relative path from your app. Or you can just use the absolute path of the mapped folder if you don't want it to be web-browsable.

Author

Commented:
sorry i need it to be web browsable
Carl TawnSenior Systems and Integration Developer

Commented:
In that case just make it a virtual directory under your site.

Author

Commented:
the CSV file right ?

But i need to be able to read it or stream read it from another server
Carl TawnSenior Systems and Integration Developer

Commented:
You said that the CSV was dumped to a folder on your SQL Server machine. So create a Virtual Directory that maps to the folder on the SQL Server where the CSV files get written to. You're mapping to the directory, not the file itself.

Author

Commented:
Ok thats fine but the problem is the code what do i write in the code to access it
Senior Systems and Integration Developer
Commented:
You just need to update your Server.MapPath() call to point at the virtual directory:
string filepath = Server.MapPath("~/Name of your virtual directory/T.csv");
using (var reader = new System.IO.StreamReader(filepath))
{
    // etc, etc
}

Open in new window

Author

Commented:
I am assuming that in the virtual directory i have to put the name and before that the IP add of the server
Carl TawnSenior Systems and Integration Developer

Commented:
You create the virtual directory in IIS admin and point it at the physical directory on your sql box. Then in code you reference the virtual directory name.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial