开发者

cant get c# to make a downloadable csv

I am having to code up a button which, onclick, downloads a csv file onto the 开发者_StackOverflow社区client's computer (this is a web app)

So, did some research, pulled all strings together, go figure, its not working.

I am trying to get this for 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 a blog that came up on google, that's all I need to get generate that csv an download it.

only, its not working and I cant figure out what's wrong with it.

1) firebug inspection: response is EMPTY.

2) VS Debugging, strangely stops after the line Response.End() and complaints that it cant find content.

Am I missing something? Thanks much. very much.

Edit: I just realized something, this is a child page and there is an update panel in the master page. Can this be the cause of the problem? if so, how can I cancel the ajax postback just for this child page?


reading your comments and edit, looks like you want to cancel ajax postback for just this one button from child page and your update panel is in master page and you can't access it.

If I have that details correct, just add this code on your child content page's PageLoad():

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

That will force full page postback and your files will be downloaded.


I would prefer doing that on a WebHandler .ashx file. Separated from the main page you are at.

<%@ 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;
        }
    }
}


From what I can see on your code it seems you are sending that response as part of a post-back. I don't know if you are using WebForms (ASPX) or MVC, but if you are using traditional ASP.NET you can create a new page called "CSV.aspx", remove the entire HTML body of the response and do something like this:

on CSV.aspx:

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

And on the code-behind (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");
    }
}

Also, notice the ContentType is not text/csv.


Try calling Response.Flush() in between Response.Write() and Response.End(). Also, don't use an <asp:Button for this. Use an <asp:HyperLinK (or even just a normal <a href anchor) that points to a different page (or, even better, a *.ashx handler) that will give this response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜