I haven’t really coded this before but I’ve got a list of file names in my view. There’s a download button next to them. So for example:

here’s the view markup for that:
@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>
}
}
When clicked, I send over the CdN url to my action method which under the hood goes and downloads that file from Rackspace and then copies it to our server for the user to download. So my action method returns back the url to the .zip file that the user has now access to download. But when I test this, it’s posting back and redirecting me to a blank page that shows the url rendered in text on the page and that’s it!
"http://www.ourAssetServer.com/assets/20120331002728.zip"
I’m not sure how to invoke a save as prompt instead and keep the user on the same View they are already in or redirect them back to the same view that lists out the files to download…
What I want to happen is this:
- User clicks on the download button
- Action Method does its thing, downloads the file in the backend
- User seemlessly gets a save as pop-up immediately after clicking the download button
this is probably routine but I’ve never done this before. I need help figuring out how to get the save as prompt to show up and how I should handle returning to my View after my action method gets the final download url for the .zip…what do I do with that url in my action method…return it to the view? Not sure here.
here’s the action method:
[HttpGet]
public string GetFileDownloadUrl(string fileCdnUrl int carId, int userId)
{
string downloadUrl = string.Empty;
downloadUrl = GetFileZipDownloadUrl(carId, userId, fileCdnUrl);
return downloadUrl;
}
downloadUrl returned in my action method is that “http://www.ourAssetServer.com/assets/20120331002728.zip” string for example sent back to the user…I just need to figure out how to get this wired up to a save as prompt but also figure out how to handle the view…do I redirect back to the same view or what?
UPDATE:
After researching a bit more, here’s what I came up with:
[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);
}
I get the error 'http://ourAssetServer.com/assets/20120331002728.zip'; is not a valid virtual path
UPDATE #2
Here’s my latest attempt using the redirect method to try and get a save prompt to show. What’s happening is it is hitting my GetFileDownloadUrl but then never seems to get to my Redirect action method and I end up being redirected to a completely blank page (all white) with the url as http://localhost/Car/GetFileDownloadUrl and just stops there
@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;
}
I’m not sure how the RedirectToUrl should be coded…the return type and what do you return if anything? Should it just redirect keeping me on the page I was on? I don’t “think” this is even being hit, only my first method. I put a debug point on the Response.Redirect and it’s never hit..only a debug point on RedirectToAction(“RedirectToUrl”, downloadUrl);
Consider defining a new action “RedirectToUrl” that takes url as parameter and then using RedirectToAction in your code:
Update: using the FilePathResult with Server.MapPath is another option (assuming that the server you’re downloading the file to is the same as server running your ASP.NET MVC code). Add the following to the bottom of function in your last edit: