3

私のページには、ファイルをダウンロードする.aspxダウンロードボタンがあります。onclick.apk

私のPCで実行すると、正常に動作し、PCに.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();
        }
    }
4

2 に答える 2

7

これが私の問題をどのように修正したかです

私のアプリケーションは、IIS 7 を持つ Windows サーバー 2008r2 でホストされています

ステップ 1: .aspx ページにハイパーリンクを追加し、navigateurl をファイル パスとして設定します。

<asp:HyperLink ID="lnkdwnload" runat="server" NavigateUrl="~/Application_Android/MyAndroidAppAame.apk">Download MyApp</asp:HyperLink>

ステップ 2: Web.config は staticContent の下に mimeMap 要素を追加します

<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".apk" mimeType="application/vnd.android.package-archive"/>
    </staticContent>
</system.webServer>
于 2012-06-19T10:44:44.810 に答える
0

これは、私が Android のネイティブ ブラウザーで直面したのと同じ問題である可能性があります。問題は、ダウンロード アクションがプラットフォームのダウンロード アプリケーション (ブラウザとは別) に渡され、ページがリロードされ、実際の APK の代わりに aspx ページがダウンロードされることです。

Opera Mobile でダウンロードしてみてください。問題が解決した場合は、おそらく同じ問題です。ボタンを標準のハイパーリンクに置き換えるのが、これに対する最も簡単な解決策です。ダウンロードするだけでなく、他のロジックも必要な場合は、オプションではないかもしれません。

于 2012-06-18T08:34:01.590 に答える