0

我需要在 mvc application 的运行时设置保存位置。在 Windows 应用程序中,我们使用

System.Windows.Forms.SaveFileDialog();

但是,我们使用什么网络应用程序呢?

4

1 回答 1

2

目前尚不清楚您要保存什么。在 Web 应用程序中,您可以使用文件输入将文件上传到服务器:

<input type="file" name="file" />

有关在 ASP.NET MVC 应用程序中上传文件的更多信息,您可以查看following post.

另一方面,如果您希望用户能够从服务器下载一些文件并提示他要保存此文件的位置,您可以从控制器操作返回文件结果并指定 MIME 类型和文件名:

public ActionResult Download()
{
    var file = Server.MapPath("~/App_Data/foo.txt");\
    return File(file, "text/plain", "foo.txt");
}

该方法还有其他重载File,允许您动态生成文件并将其作为流传递给客户端。但是,从服务器下载文件时,在 Web 应用程序中要了解的重要部分是Content-Disposition标头。它有 2 个可能的值:inlineattachment。例如,使用上面的代码,以下标头将添加到响应中:

Content-Type: text/plain
Content-Disposition: attachment; filename=foo.txt

... contents of the file ...

当浏览器从服务器接收到这个响应时,它会提示用户一个“另存为”对话框,允许他在他的计算机上选择存储下载文件的位置。


更新:

以下是在 Web 应用程序中实现类似功能的方法:

public ActionResult Download()
{
    var file1 = File.ReadAllLines(Firstfilpath);
    var file2 = File.ReadAllLines(2ndfilpath);
    var mergedFile = string.Concat(file1, file2);
    return File(mergedFile, "text/plain", "result.txt");
}
于 2013-04-11T05:49:20.427 回答