0

I have the following code that returns a .CSV file when a link is clicked for a user to save:

public FileContentResult DownloadCSV()
{
    StringBuilder sb = new StringBuilder();
    var props = typeof(PenCalcModel).GetProperties();
    var penSummary = PenSummaryReport(new DateTime(DateTime.Today.Ticks, DateTimeKind.Utc), new DateTime(DateTime.Today.AddDays(1).Ticks, DateTimeKind.Utc), "");

    foreach (PenCalcModel item in penSummary)
    {
        sb.Append(string.Join(",", props.Select(p=>p.GetValue(item,null).ToString())));
        sb.AppendLine();
    }

    return File(new System.Text.UTF8Encoding().GetBytes(sb.ToString()), "text/csv", "PenReport.csv");
}

Is there a way where I could set it up to have this exact same .CSV file exported from the server to a networked drive every X hours?

Kittoes0124
  • 4,930
  • 3
  • 26
  • 47
  • Yep, have user click on the link every X hours! Kidding. Could you please clarify what you mean by "exported"? Exported from where to where? If by export you mean download, then download is initiated from a client so you can't do anything on the server to help that. On the client on the other hand you can create a job, a service, a script that will do x hourly downloads for you. – Andrew Savinykh Jun 20 '12 at 01:50
  • @zespri Export from the server to a networked drive. Even more specifically: the web app runs a LINQ-to-Entities query on a Sybase database, I turn the resulting IEnumerable into this .CSV file. – Kittoes0124 Jun 20 '12 at 01:53
  • 1
    The easiest (in terms of development efforts) would be to create a stand-alone app to do the export and set up Windows Task Scheduler job to ran it on schedule. – Andrew Savinykh Jun 20 '12 at 01:57
  • @zespri Good call, that took me all of 5 minutes to do. – Kittoes0124 Jun 20 '12 at 02:12

2 Answers2

3

Having anything set as a scheduled task/job should not be the responsibility of a web app. The app could be recycled at any time and could even interrupt that job. Those kinds of implementations should be the responsibility of a windows service or some sort of task based job on the server. Short answer, don't do this in the web app.

CD Smith
  • 6,597
  • 7
  • 40
  • 66
  • Ok, so how would I go about doing this outside the web app? – Kittoes0124 Jun 20 '12 at 01:40
  • 1
    You should look into creating a Windows Service if you are running a Windows server. You could use SQL to build a file for you or SQL Reporting services, even a batch file that gets executed with a scheduled task on the server. – CD Smith Jun 20 '12 at 01:43
1

I would probably use a powershell script to download the content and a scheduled windows task to trigger it. There's several examples of downloading content in this question: download url content

Community
  • 1
  • 1
Ted Elliott
  • 3,415
  • 1
  • 27
  • 30
  • Not at all what I'm looking for but that's interesting. The content on the page is formatted in a way that's easy to browse/read. The content in my download link is formatted for easy manipulation. This is because the average user will just want to go through and quickly glance at my data and a power user will likely want to download it and transform it into something more useful to their particular wants/needs. – Kittoes0124 Jun 20 '12 at 02:22