0

我想读取一个日志文件(txt 文件)并在 txt 文件中查看它并以 txt 格式保存。这是我的代码,它可以很好地显示和下载 txt 文件中的读取内容,我在这方面几乎没有问题..

public FileResult Download(string id)
    {
        int rowId = Convert.ToInt32(id);
        LoadFileInfoCache();
        var fileDetails = from ff in _currentFileDetails
                          where ff.FileId == rowId
                          select new
                          {
                              name = ff.FileName,
                              location = ff.FileLocation
                          };
        var fileDetailsList = fileDetails.ToList();
        string fileLocation = fileDetailsList[0].location;
        string fileName = fileDetailsList[0].name;            
        string contentType = "application/txt";
        var file = System.IO.File.Open(fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        var stream = (Stream)(file);
        return File(stream, contentType, fileName);
    }

当我单击保存按钮时,在另存为窗口中我想要带有扩展名的文件名。但它只为我显示文件名。而且在文本文件中,标题名称应该是 filename.txt 但它显示了我的代码的 Filename[1]。任何人都可以给出一些想法,在标题中显示带有 extn 的文件名并另存为窗口。

4

1 回答 1

1

更新代码

public FileResult FileOutput()
            {
                string filename = "alokdida.txt";
                string filepath = "C:\\logs\\Structured_Exception_Log.txt";

                return File(filepath, "application/octet-stream", filename);
            }

这应该适合你。

第二次编辑(以粗体查看更改的代码)

public FileResult Download(string id)
    {
        int rowId = Convert.ToInt32(id);
        LoadFileInfoCache();
        var fileDetails = from ff in _currentFileDetails
                          where ff.FileId == rowId
                          select new
                          {
                              name = ff.FileName,
                              location = ff.FileLocation
                          };
        var fileDetailsList = fileDetails.ToList();
        string fileLocation = fileDetailsList[0].location;
        string fileName = fileDetailsList[0].name+".txt"; // Here you need to append the .txt
        string contentType = "application/txt";
        var file = System.IO.File.Open(fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        var stream = (Stream)(file);
        return File(stream, contentType, fileName);
    }
于 2012-07-02T11:23:24.627 回答