Reputation: 35
The code that I have listed here works when I ReadAllText from a local file. What I need to be able to do is replace the path "C:\LocalFiles\myFile.csv" with "https://mySite.blah/myFile.csv".
I have tried several methods, but can't seem to be able to get the csv file loaded into a string variable. If I could just do that, then the code would work for me.
var csv = System.IO.File.ReadAllText(@"C:\LocalFiles\myFile.csv");
StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(csv).WithFirstLineHeader())
{
p.Configuration.NullValue = null;
if (p.Configuration.CSVRecordFieldConfigurations.IsNullOrEmpty())
{
p.Configuration.NullValue = null;
}
// ChoIgnoreFieldValueMode.DBNull = ChoIgnoreFieldValueMode.Empty();
using (var w = new ChoJSONWriter(sb))
w.Write(p);
}
string fullJson = sb.ToString();
If I simply replace the path, I get an error message that says that the path is invalid.
Upvotes: 0
Views: 1049
Reputation: 868
You need to get the string using a web request:
string urlAddress = "https://mySite.blah/myFile.csv";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream,
Encoding.GetEncoding(response.CharacterSet));
}
string data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
or using a Webclient:
WebClient wc = new WebClient();
string data = wc.DownloadString("https://mySite.blah/myFile.csv");
Then pass data
into your reader instead of using the System.IO.File.ReadAllText(@"C:\LocalFiles\myFile.csv");
Both of the above examples assume that the file is publicly accessible at that url without authentication or specific header values.
Upvotes: 2
Reputation: 6332
Replace this line
var csv = System.IO.File.ReadAllText(@"C:\LocalFiles\myFile.csv");
with
string result = client.GetStringAsync("https://mySite.blah/myFile.csv").Result;
or
var textFromFile = (new WebClient()).DownloadString("https://mySite.blah/myFile.csv");
There are many other ways to do it as well. Just google it.
Upvotes: 0