Can not get c # to make downloadable csv

I need to encode a button that, onclick, downloads a csv file to a client computer (this is a web application)

So, some kind of research, pulled out all the lines together, go with the number, not working.

I am trying to get this now:

    protected void btnDownload_onClick(object sender, EventArgs e)
    {
        Response.Clear(); 
        Response.AddHeader("content-disposition", "attachment; filename=tmp.csv");
        Response.ContentType = "text/csv";
        Response.Write("one,two,three,four,five");
        Response.End();
    }

according to the blog that appeared on google that all i need is to get that csv download it.

it doesn’t work, and I can’t understand what is wrong with him.

1) firebug inspection: the answer is EMPTY.
2) VS Debugging, strangely stops after a line Response.End()and complaints that she cannot find the content.

Am I missing something? Many thanks. highly.

: , , . ? , ajax ?

+5
4

, , ajax , , 't .

, PageLoad():

        ScriptManager sm = ScriptManager.GetCurrent(Page);
        if (sm != null)
            sm.RegisterPostBackControl(btnDownload); 
        //btnDownload = the button ID, just copied from your code.

, .

+5

WebHandler.ashx. , .

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.AddHeader("content-disposition", "attachment; filename=file.csv");
        context.Response.ContentType = "text/csv";
        context.Response.Write("1,2,3,4,5,6");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
+3

, , , -. , WebForms (ASPX) MVC, ASP.NET, "CSV.aspx", HTML- - :

CSV.aspx:

<%@ Page Title="" Language="C#" AutoEventWireup="true"
    CodeBehind="CSV.aspx.cs" Inherits="TestASPNET.CSV" %>

(CSV.aspx.cs):

public partial class CSV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.AddHeader("content-disposition", "attachment; filename=tmp.csv");
        Response.ContentType = "application/octet-stream";
        Response.Write("one,two,three,four,five");
    }
}

, ContentType text/csv.

0

Response.Flush() Response.Write() Response.End(). , <asp:Button. <asp:HyperLinK ( <a href), (, , *.ashx), .

0

All Articles