asp.net filehelpers write file to client directly

邮差的信 提交于 2019-12-10 21:20:10

问题


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.


回答1:


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.




回答2:


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;
    }


来源:https://stackoverflow.com/questions/15573598/asp-net-filehelpers-write-file-to-client-directly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!