使用Android手机浏览器从Asp.net网站下载.apk时出错

Sat*_*ngh 3 .net c# asp.net android apk

在我的.aspx页面上,我有下载按钮,onclick下载.apk文件.

当我在我的电脑上运行它工作正常.apk文件下载到我的电脑上.
但是,当我使用我的Android手机进入该网站并单击下载按钮时,它将开始下载但文件点击提供错误解析包时出现问题.

实际文件大小也是604kb(从andorid手机下载时给出22kb)

下载的文件(22kb)包含html内容.

 private void DownloadFile()
    {
        string getPath = "demo_Android/demoAndroid.apk";
        System.IO.Stream iStream = null;

        // Buffer to read 10K bytes in chunk:
        byte[] buffer = new Byte[1024];

        // Length of the file:
        int length;

        // Total bytes to read:
        long dataToRead;

        // Identify the file to download including its path.
        string filepath = Server.MapPath(getPath);

        // Identify the file name.
        string filename = System.IO.Path.GetFileName(filepath);
        try
        {
            // Open the file.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read, System.IO.FileShare.Read);


            // Total bytes to read:
            dataToRead = iStream.Length;
            Response.ContentType = "application/vnd.android.package-archive";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 1024);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Response.Flush();

                    buffer = new Byte[1024];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            // Trap the error, if any.
            Response.Write("Error : " + ex.Message);
        }
        finally
        {
            if (iStream != null)
            {
                //Close the file.
                iStream.Close();
            }
            Response.Close();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sat*_*ngh 7

这是我如何解决我的问题

我的应用程序托管在具有IIS 7的Window server 2008r2下

步骤1:在.aspx页面中添加超链接设置navigateurl作为文件路径

<asp:HyperLink ID="lnkdwnload" runat="server" NavigateUrl="~/Application_Android/MyAndroidAppAame.apk">Download MyApp</asp:HyperLink>
Run Code Online (Sandbox Code Playgroud)

第2步:Web.config在staticContent下添加mimeMap元素

<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".apk" mimeType="application/vnd.android.package-archive"/>
    </staticContent>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

查看博客文章:使用移动浏览器下载.apk文件Asp.net网站