Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Export To CSV - Save As dialogue box not working in Chrome and Firefox

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!!!

like image 926
Shubh Avatar asked Oct 21 '22 22:10

Shubh


2 Answers

@shubh have you tried How to force Chrome to open an "open file dialog" when downloading a file vía ASP.NET codebehind? second solution they have put image in where they show how to open dialog box in chrome. I have chrome Version 30.0.1599.101 m in that if you go to setting in that advance setting then down you will find check box which was given in above link answer, that will solve your problem I think. If still not working then might be problem with your browser just update it to latest version then try again. Edit 1: If you put your file name extension .xls then it will open in excel for csv you need to put file name as FileName = "sample.csv", then it will open in csv format.

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewtoCSVExport.csv");
Response.Charset = string.Empty;
Response.ContentType = "application/text";

for more check this http://surajdeshpande.wordpress.com/2013/09/03/export-gridview-data-to-csv-file-in-asp-net/

like image 148
Satish ratnaparkhi Avatar answered Nov 02 '22 23:11

Satish ratnaparkhi


If the user has configured his browser to automatically download files, there's absolutely nothing you could do on the server to force this dialog to appear. I am afraid that what you are trying to achieve is impossible.

like image 29
Darin Dimitrov Avatar answered Nov 02 '22 22:11

Darin Dimitrov