Я разрабатывающая ASP.NET MVC - приложение и имеющей следующий вопрос: Когда пользователь вводит конкретный URL, в моем случае:ASP MVC - Загрузить файл, а затем перейти к представлению
http://localhost/Download
Я хочу, чтобы перейти к методу действия, загрузите файл, а затем перейти к соответствующей цели методы действия:
Моему код выглядит следующим образом:
public ActionResult Index()
{
string filePath = WebConfigurationManager.AppSettings["Download"];
FileInfo file = new FileInfo(filePath);
if (file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
Response.TransmitFile(file.FullName);
Response.Flush();
Response.End();
}
return View();
}
Моего вид л ooks так же просто, как он получает:
@{
ViewBag.Title = "Index";
}
<h2>Downloading ...</h2>
Когда я бегу, я получаю файл, но это не перейти к просмотру, а вместо этого остается в предыдущем URL.
EDIT - ANSWER: Благодаря @Abbas Galiyakot за его ответ.
Вот обновленный код, который делает то, что я хотел:
public ActionResult Index()
{
return View();
}
public ActionResult DownloadFile()
{
string filePath = WebConfigurationManager.AppSettings["Download"];
FileInfo file = new FileInfo(filePath);
if (file.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
Response.TransmitFile(file.FullName);
Response.Flush();
Response.End();
}
return new EmptyResult();
}
@{
ViewBag.Title = "Index";
}
<h2>Downloading...</h2>
<script type="text/javascript">
window.location.href = '@Url.Action("DownloadFile", "Home")';
</script>
Спасибо Аббас! Вы это хорошо объяснили! – DimaK