Sunday, November 24, 2013

Android Browser File Down Load

Android browser will not download file in button Post events. In post events the file will be some .htm garbage file. to over come this do as below.

In download button click


 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("download-file.aspx");
    }

and on  download-file.aspx file do as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mobile_download_file : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string filename = "usermanual.pdf";
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + "" + filename + "");
        Response.Write(Server.MapPath(Request.ApplicationPath) + "\\" + filename);
        Response.TransmitFile(Server.MapPath(Request.ApplicationPath) + "\\" + filename);
        Response.End();
    }
}

2 comments:

  1. Nice example, but it doesnt work on most built in browsers, because communication to server looks like:
    1)GET download.aspx and then some internal trigger(on contenttype change) call:
    2)POST download.aspx
    problem is, that response.End() break the first "connection", so in browser it will break 2) too.

    no matter if you specify Response.Redirect("",false) or Response.RedirectPermenent...

    in desktop browsers ff,opera theres no problem, only 1) occur.
    tested on asp .net 4.5 and fiddler2

    ReplyDelete