asp.net filehelpers直接写入文件,客户端 [英] asp.net filehelpers write file to client directly

查看:175
本文介绍了asp.net filehelpers直接写入文件,客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了C#.NET项目FileHelpers库。所有好的工作,除了我需要通过点击一个按钮有生成CSV可保存。

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.

因此​​,用户点击一个按钮,他们得到一个提示保存文件。我知道Filehelpers有WriteStream的选择,但他们自己的文档显示WriteFile的唯一的一个例子。我不知道,如果是WriteStream需要的什么,但我认为它是。

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.

任何人都知道,如果它是可能有直接保存到客户端的CSV文件,而不是服务器硬盘?

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

感谢。

更多的细节更新:

我会尽量给更多的细节有助于澄清这就是我想要的目的。我不希望保存在服务器上的任何文件。我使用FileHelpers生成一个记录,我想尝试用自己的方法WriteStream写该记录为CSV直接到他们的浏览器;但他们提供的例子并不实际使用该方法在所有的,奇怪的。

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

和这里的问题的方法。

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.

这可能是因为我必须去通过正常的途径来实现这一点,但我看到了WriteStream方法和想,也许FileHelpers促进实现相同的结果的一个很好的清洁方法。

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.

希望这是更清晰。

推荐答案

您应该能够做到像下面这样:

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.

修改

我认为你缺少的步骤是:

I think the steps you are missing are:

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

然后你可以使用

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

而不是的Response.Write(csv.ToString())在我上面的第一个例子。

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

这篇关于asp.net filehelpers直接写入文件,客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆