I am trying Export csv
file to the User with Open/Save option.
My issue is some what similar to how-to-force-chrome-to-open-an-open-file-dialog-when-downloading-a-file-via-as(It is downloading the file in Chrome and Firefox)
, I have tried with the solution suggested by @Dev
but it is not working.
I wrote my code as below:-
return File(new System.Text.UTF8Encoding().GetBytes(csvData),
"text/csv", filename);
But, it was not working in Chrome. The file is getting downloaded by default.
Then after googling , I found returning-a-file-to-view-download-in-mvc, from which I was trying to do something like below:-
var csvData = "hello";// I am filling this variable with ,y values from DB!
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = "test",
Inline = false,
};
Response.AppendHeader("Content-Disposition",
cd.ToString());
return File(new System.Text.UTF8Encoding().GetBytes(csvData),
"text/csv");
but still it was downloading the file in Chrome. then I came across how-to-display-open-save-dialog-asp-net-mvc-4, where @JoãoSimões
mentioned as:-
That is browser dependent. If you set to download automatically to a given folder, the browser will download automatically. Firefox and Chrome are some browsers with this behavior. – João Simões Jan 3 at 13:09
If the above is true, then how can I overcome my problem? How can I get the open/save dialogue ? I am unable to Export my CSV with open/save option.
Edit 1
I was trying to do something like this (got it here):-
public class ExcelResult : ActionResult
{
public string FileName { get; set; }
public string Path { get; set; }
public string Data { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Buffer = true;
context.HttpContext.Response.Clear();
context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
context.HttpContext.Response.ContentType = "text/csv";
context.HttpContext.Response.Write(new System.Text.UTF8Encoding().GetBytes(Data));
}
}
and My controller code:-
return new ExcelResult
{
FileName = "sample.xls",
Path = "",
Data = csvData
};
but still, it is downloading the Excel ...
Edit 2
Tried opening the excel with HttpContext.Current.Response
/// <summary>
/// Export CSV
/// </summary>
/// <returns></returns>
public void DownloadCSV()
{
try
{
var csvData = Session["CSVData"].ToString();
byte[] getContent = new System.Text.UTF8Encoding().GetBytes(csvData);
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", getContent.Length.ToString());
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "testing.csv");
System.Web.HttpContext.Current.Response.BinaryWrite(getContent);
System.Web.HttpContext.Current.Response.Flush();
}
catch (Exception ex)
{
HttpResponseMessage message = new HttpResponseMessage()
{
Content = new StringContent("Error Exporting Data")
};
throw new System.Web.Http.HttpResponseException(message);
}
}
but, still not working!!!