若只是提供一個固定的 APK 或任意檔案下載,純 HTML 錨點連結 即可,無需任何後端程式碼:
<a href="files/app-release.apk" download="MyApp_v1.0.apk">下載 APK 安裝檔</a>
href:相對或絕對路徑指向伺服器上的實體檔案。download 屬性 (HTML5):建議瀏覽器直接下載而非在新分頁開啟,並可指定下載檔名。files/ 或 downloads/ 資料夾,確保 IIS/ASP.NET Core 靜態檔案中介軟體啟用。若需「登入驗證後才能下載」、「動態打包 ZIP」、「支援斷點續傳 (Range Request)」、「記錄下載日誌」,則需撰寫控制器/處理常式:
[HttpGet("download/{fileId}")]
public async Task Download(int fileId) {
var fileMeta = await _fileService.GetMetaAsync(fileId);
if (fileMeta == null) return NotFound();
var path = _storage.Path(fileMeta.StoredPath);
if (!System.IO.File.Exists(path)) return NotFound();
// 支援 Range 斷點續傳
return PhysicalFile(path, "application/octet-stream", fileMeta.OriginalName);
}
public ActionResult Download(string id) {
var path = Server.MapPath("~/App_Data/" + id + ".apk");
return File(path, "application/vnd.android.package-archive", "app.apk");
}
?path=../../web.config 類路徑遍歷攻擊,使用資料庫 ID 映射實體路徑。File.ReadAllBytes 導致記憶體暴增,ASP.NET Core PhysicalFileResult 內建串流與 Range 支援。application/vnd.android.package-archive,通用二進制用 application/octet-stream。靜態檔案直連最簡單;動態權控用 Controller 回傳 FileResult/PhysicalFileResult;永遠記得驗證授權、防止路徑遍歷、支援大檔串流。