Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 18th, 2012  |  syntax: None  |  size: 2.52 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Error Downloading .apk from Asp.net website using android phone browser
  2. private void DownloadFile()
  3.     {
  4.         string getPath = "demo_Android/demoAndroid.apk";
  5.         System.IO.Stream iStream = null;
  6.  
  7.         // Buffer to read 10K bytes in chunk:
  8.         byte[] buffer = new Byte[1024];
  9.  
  10.         // Length of the file:
  11.         int length;
  12.  
  13.         // Total bytes to read:
  14.         long dataToRead;
  15.  
  16.         // Identify the file to download including its path.
  17.         string filepath = Server.MapPath(getPath);
  18.  
  19.         // Identify the file name.
  20.         string filename = System.IO.Path.GetFileName(filepath);
  21.         try
  22.         {
  23.             // Open the file.
  24.             iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
  25.                         System.IO.FileAccess.Read, System.IO.FileShare.Read);
  26.  
  27.  
  28.             // Total bytes to read:
  29.             dataToRead = iStream.Length;
  30.             Response.ContentType = "application/vnd.android.package-archive";
  31.             Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
  32.  
  33.             // Read the bytes.
  34.             while (dataToRead > 0)
  35.             {
  36.                 // Verify that the client is connected.
  37.                 if (Response.IsClientConnected)
  38.                 {
  39.                     // Read the data in buffer.
  40.                     length = iStream.Read(buffer, 0, 1024);
  41.  
  42.                     // Write the data to the current output stream.
  43.                     Response.OutputStream.Write(buffer, 0, length);
  44.  
  45.                     // Flush the data to the HTML output.
  46.                     Response.Flush();
  47.  
  48.                     buffer = new Byte[1024];
  49.                     dataToRead = dataToRead - length;
  50.                 }
  51.                 else
  52.                 {
  53.                     //prevent infinite loop if user disconnects
  54.                     dataToRead = -1;
  55.                 }
  56.             }
  57.         }
  58.         catch (Exception ex)
  59.         {
  60.             // Trap the error, if any.
  61.             Response.Write("Error : " + ex.Message);
  62.         }
  63.         finally
  64.         {
  65.             if (iStream != null)
  66.             {
  67.                 //Close the file.
  68.                 iStream.Close();
  69.             }
  70.             Response.Close();
  71.         }
  72.     }
  73.        
  74. <asp:HyperLink ID="lnkdwnload" runat="server" NavigateUrl="~/Application_Android/MyAndroidAppAame.apk">Download MyApp</asp:HyperLink>
  75.        
  76. <system.webServer>
  77.     <staticContent>
  78.       <mimeMap fileExtension=".apk" mimeType="application/vnd.android.package-archive"/>
  79.     </staticContent>
  80. </system.webServer>