Avoiding content type issues when downloading a file via browser on Android

后端 未结 5 1771
無奈伤痛
無奈伤痛 2020-11-29 02:24

If I have a file made available to a browser through my webapp, I normally just set the URL to something like http://website.com/webapp/download/89347/image.jpg

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 02:48

    As I wrote at downloading files from android:

    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();
        }
    }
    
    the same can be implemented in php also.
    

提交回复
热议问题