在我的.aspx
頁面中,我有下載按鈕,其中onclick
下載.apk
文件。Error使用Android手機瀏覽器從Asp.net網站下載.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();
}
}
,你可以下載你的文件,當你直接去下載鏈接,HTTP://yourwebsite/demo_Android/demoAndroid.apk – TimVK
這不是錯誤,但我認爲在它的每一個沖水1K太快了!也沒有理由在每個循環上重新創建緩衝區。有沒有任何情況下,Android有保護,不允許下載.apk文件? – Aristos
這行也可能會產生你的錯誤'Response.Write(「Error:」+ ex.Message);' - 你需要記錄你的錯誤。如果你發送數據中間,這個錯誤文本,然後你的包被打破。 – Aristos