爲了保持您的目錄結構平面存儲您的文件通過追加一個GUID(如你已經做過的)。在您的下載處理程序(控制器操作方法)中,首先通過從文件名中刪除GUID將基於GUID的文件名轉換爲原始文件名。然後使用FileContentResult
類來傳輸文件。您可以設置FileDownloadName
屬性以指定要傳輸的文件的文件名。實際上,FileDownloadName
屬性將Content-Disposition標頭設置在底層。
這裏是一個小的代碼示例(您下載控制器類的動作方法):
string fileToDownload = "test.jpg_4274B9D4-9084-441C-9617-EAD03CC9F47F";
string originalFileName = fileToDownload.Substring(0, fileToDownload.LastIndexOf('_'));
FileContentResult result = new FileContentResult(
System.IO.File.ReadAllBytes(Server.MapPath(String.Format("~/files/{0}", fileToDownload))), "application/binary");
result.FileDownloadName = originalFileName; // Sets the Content-Disposition header
return result;
用戶下載文件時提示打開/保存原文件名的文件。
希望,這有助於。