我以前沒有真正編碼過,但我在我的視圖中有一個文件名列表。他們旁邊有一個下載按鈕。因此,例如:如何調用保存作爲返回文件路徑的提示
這裏是該視圖標記:
@foreach (CarFileContent fileContent in ModelCarFiles)
{
using (Html.BeginForm("GetFileDownloadUrl", "Car", FormMethod.Get, new { carId = Model.CarId, userId = Model.UserId, @fileCdnUrl = fileContent.CdnUrl }))
{
@Html.Hidden("userId", Model.UserId);
@Html.Hidden("carId", Model.CarId);
@Html.Hidden("fileCdnUrl", fileContent.CdnUrl);
<p><input type="submit" name="SubmitCommand" value="download" /> @fileContent.Name</p>
}
}
點擊後,我送過來的CDN網址到我的行動方法,它的引擎蓋下進入以及從文件下載Rackspace,然後將其複製到我們的服務器供用戶下載。所以我的操作方法將url返回給用戶現在可以下載的.zip文件。但是當我測試這個時,它會回傳並將我重定向到一個空白頁面,該空白頁面顯示頁面中文本呈現的URL,就是這樣!
"http://www.ourAssetServer.com/assets/20120331002728.zip"
我不知道如何調用保存的提示,而不是和保持用戶在同一個視圖,這些都已經或重定向他們回到列出了要下載的文件相同的觀點...
我希望發生的是:
- 下載按鈕用戶點擊
- 操作方法做它的東西,在下載後端文件
- 用戶在點擊下載按鈕後立即彈出保存爲
這可能是常規,但我從來沒有這樣做過。我需要幫助搞清楚如何獲取保存爲提示顯示,以及我的操作方法獲取.zip的最終下載url後我應該如何處理返回到我的視圖...我在做什麼我的行動中的網址方法...將其返回到視圖?不確定在這裏。
這裏的操作方法:
[HttpGet]
public string GetFileDownloadUrl(string fileCdnUrl int carId, int userId)
{
string downloadUrl = string.Empty;
downloadUrl = GetFileZipDownloadUrl(carId, userId, fileCdnUrl);
return downloadUrl;
}
downloadUrl在我的操作方法返回的是「http://www.ourAssetServer.com/assets/20120331002728.zip」字符串,例如發送回用戶......我只需要弄清楚如何得到這個有線保存爲提示,但也弄清楚如何處理視圖...我重定向回到相同的觀點或什麼? UPDATE:
研究多一點之後,這裏就是我想出了:
[HttpGet]
public FilePathResult GetFileDownloadUrl(string fileCdnUrl, int carId, int userId)
{
string downloadUrl = string.Empty;
downloadUrl = rackSpaceTask.GetFileZipDownloadUrl(carId, userId, fileCdnUrl);
int i = downloadUrl.LastIndexOf("/");
string fileName = downloadUrl.Substring(i);
return File(downloadUrl, "application/zip", fileName);
}
我得到的錯誤'http://ourAssetServer.com/assets/20120331002728.zip'; is not a valid virtual path
更新#2
這裏是我的最新嘗試使用重定向方法嘗試獲取保存提示以顯示。發生了什麼事是它打我GetFileDownloadUrl但隨後似乎永遠不會讓我的重定向操作方法,我最終會被重定向到一個完全空白頁(全白)與URL作爲http://localhost/Car/GetFileDownloadUrl,只是停在那裏
@foreach (CarContent fileContent in Model.CarFiles)
{
using (Html.BeginForm("GetFileDownloadUrl", "Car", FormMethod.Post, new { carId = Model.CarId, userId = Model.UserId, @fileCdnUrl = fileContent.FileCdnUrl }))
{
@Html.Hidden("userId", Model.UserId);
@Html.Hidden("carId", Model.CarId);
@Html.Hidden("fileCdnUrl", fileContent.FileCdnUrl);
<p><input type="submit" name="SubmitCommand" value="download" /> @fileContent.Name</p>
}
}
public void GetFileDownloadUrl(string fileCdnUrl, int carId, int userId)
{
string cownloadUrl = string.Empty;
cownloadUrl = GetFileDownloadUrl(carId, userId, fileCdnUrl);
RedirectToAction("RedirectToUrl", downloadUrl);
}
public ActionResult RedirectToUrl(string fileUrl)
{
Response.Redirect(fileUrl);
return null;
}
我不確定RedirectToUrl應該如何編碼......返回類型以及返回類型是什麼?它應該只是重定向保持我在網頁上?我不「認爲」這是甚至被擊中,只有我的第一種方法。我在Response.Redirect上放了一個調試點,它從來沒有被擊中..只是RedirectToAction上的一個調試點(「RedirectToUrl」,downloadUrl);
我爲什麼要這樣做?只是想知道 – PositiveGuy 2012-03-31 06:03:23
這將允許你重定向你的用戶到文件的URL(即http://ourAssetServer.com/assets/20120331002728.zip),這將導致瀏覽器打開另存爲對話框等取決於瀏覽器設置。這隻有在客戶端可以訪問服務器(ourAssetServer.com)時纔有效。 – BluesRockAddict 2012-03-31 06:20:43
RedirectToUrl會是什麼,一個動作方法?那裏面會做什麼? – PositiveGuy 2012-03-31 06:39:33