How to get the content of a ViewResult in MVC?
-
08-03-2021 - |
質問
I have a view that is rendered with ContentType of "application/msword". For users this appears as download file. Now, I would like to save this document server side. How could get I access the rendered document server side ?
解決
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
var viewContext =
new ViewContext(
filterContext.Controller.ControllerContext,
vResult.View,
vResult.ViewData,
vResult.TempData,
writer);
vResult.View.Render(viewContext, writer);
writer.Flush();
}
他のヒント
On the server you could use a WebClient to fire an HTTP request to the url and download the file or download it in memory as a byte array.
所属していません StackOverflow