Created
October 5, 2018 14:13
-
-
Save lfgrando/5e2ed1484aa6523d27962ce279fc6019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static TextFieldParser GetTextFieldParser(string source) | |
{ | |
if (!source.IsUrl()) | |
return new TextFieldParser(source); | |
var file = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "files", $"{DateTime.Now:yyyyMMddHHmmssfff}", Path.GetFileName(source)); | |
var path = Path.GetDirectoryName(file); | |
if (!Directory.Exists(path)) | |
Directory.CreateDirectory(path); | |
// Save to file before calling the parser | |
using (var response = _httpClient.GetAsync(source).GetAwaiter().GetResult()) | |
{ | |
response.EnsureSuccessStatusCode(); | |
using (var stream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()) | |
using (var fileStream = File.Create(file)) | |
{ | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.CopyTo(fileStream); | |
} | |
return new TextFieldParser(file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment