3

I'm using the FileHelpers library for a C# .net project. All is working ok except I need to have the generated CSV savable via a button click.

So the user clicks a button and they get a prompt to save the file. I know Filehelpers has a WriteStream option but their own documentation shows only an example of WriteFile. I don't know if WriteStream is what's needed but I think it is.

Anyone know if it's possible to have the CSV file saved directly to the client rather than to the server hard drive?

Thanks.

UPDATE WITH MORE DETAIL:

I'll try to give more detail to help clarify what it is I'm trying to achieve. I don't want to save any file on the server. I am using FileHelpers to generate a record and I want to try and use their WriteStream method to write that record as a CSV directly to their browser; but the example they provide doesn't actually use that method at all, strangely.

Here's the link to their method:

http://www.filehelpers.com/FileHelpers.FileHelperEngine.WriteStream_overload_2.html

And here's the method in question.

public void WriteStream(
   TextWriter writer,
   IEnumerable records,
   int maxRecords
);

I can easily save the file to the server but don't want to as I've no need for it and want to get the client to save it directly to their own machine.

It may be that I have to go through the normal route to achieve this but I saw the WriteStream method and thought maybe FileHelpers facilitated a nice clean way of achieving the same result.

Hope this is clearer.

Full Time Skeleton
  • 1,680
  • 1
  • 22
  • 39
  • Can you post working code that you currently have saving a file should not be a problem but it's really hard to determine what you are trying to do without seeing current code that you have. If this is `ASP.net` application then you want you use the `Response Object` as well as `StreamWriter Class` – MethodMan Mar 22 '13 at 15:07
  • I don't really have any code left (have written and deleted it a thousand times at this stage). What I'm trying to do is get a button click to return a CSV file to the user through their browser. I can save it to the server no problem, but getting the equivalent of Response.TransmitFile() to work without having the file already saved eludes me. hope this is clearer. – Full Time Skeleton Mar 22 '13 at 15:12
  • you can still do this you need to do it using `Response` here is link that you can reference it's pretty straight forward http://stackoverflow.com/questions/10974930/how-to-save-created-excel-file-to-client-pc-in-asp-net change the `Response.ContentType = "application/ms-excel";` to `Response.ContentType = "application/text";` or `Response.ContentType = "text/csv";` – MethodMan Mar 22 '13 at 15:15
  • Thanks, I know about writing to the client in the way you mention but I think that FileHelpers library has some equivalent that I could use. Maybe it doesn't and I'll need to use the process you linked to, but I could have sworn they did offer some version of the same process. – Full Time Skeleton Mar 22 '13 at 15:20

2 Answers2

2

You should be able to do something like the following:

Response.ContentType = @"application/x-msdownload";
Response.AppendHeader("content-disposition", "attachment; filename=" + FILE_NAME);

Response.Write(csv.ToString());
Response.Flush();
Response.End();

There are plenty of more elaborate suggestions on StackOverflow such as this one. It depends what you are trying to achieve.

EDIT

I think the steps you are missing are:

MemoryStream stream = new MemoryStream();
StreamWriter streamWriter = new StreamWriter(stream);
engine.WriteStream(streamWriter, records);
stream.Position = 0;

and then you can use

StreamReader reader = new StreamReader(stream);
Response.Write(reader.ReadToEnd);

instead of Response.Write(csv.ToString()) in my first example above.

Community
  • 1
  • 1
shamp00
  • 11,106
  • 4
  • 38
  • 81
0

Don't forget to flush StreamWriter. See example

public MemoryStream CreateCsvFileAsMemoryStream(List<TestItem> testItems) {
        var engine = new FileHelperEngine<TestItemCsvMapping>();
        var items = testItems.Select(ti => (TestItemCsvMapping)ti).ToList();

        engine.HeaderText = engine.GetFileHeader();
        var ms = new MemoryStream();
        var sw = new StreamWriter(ms);
        engine.WriteStream(sw, items);
        sw.Flush();

        //var reader = new StreamReader(ms);
        //ms.Position = 0;
        //Console.Write(reader.ReadToEnd());

        ms.Position = 0;

        return ms;
    }
Stefan Varga
  • 479
  • 6
  • 6