Reputation: 220
I want to read a log file(txt file) and view this in txt file and save with txt format. This is my code,which is working fine for showing and downloading the read content in txt file,i have few issues in that..
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);
}
when i click save button, in save as window i want filename with extension.but it shows only filename for me. and also in textfile the header name should be filename.txt but it it shows Filename[1] for my code. can anyone please give some idea to show Filename with extn in header and save as window.
Upvotes: 0
Views: 2667
Reputation: 1733
Updated code
public FileResult FileOutput()
{
string filename = "alokdida.txt";
string filepath = "C:\\logs\\Structured_Exception_Log.txt";
return File(filepath, "application/octet-stream", filename);
}
This should work for you.
2nd time edit (see changed code in bold font)
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);
}
Upvotes: 1