Download file from the controller

In the Aspnet5 RC1 web application update1 trying to do the same thing as Response.BinaryWrite, loading the file in the old AspNet application. The user should receive a pop-up save dialog on the client side.

when the following code is used, a pop-up prompt appears on the client side to save the file.

public void Configure(IApplicationBuilder app)
{
    //app.UseIISPlatformHandler();
    //app.UseStaticFiles();
    //app.UseMvc();
    app.Run(async (objContext) =>
    {
        var cd = new System.Net.Mime.ContentDisposition { 
                     FileName = "test.rar", Inline = false };
        objContext.Response.Headers.Add("Content-Disposition", cd.ToString());
        byte[] arr = System.IO.File.ReadAllBytes("G:\\test.rar");
        await objContext.Response.Body.WriteAsync(arr, 0, arr.Length);
    });
}

But when the same code is used inside the Controller action, app.Run is commented out, the preservation of the pop-up window is not displayed, the content is simply displayed as text.

[Route("[controller]")]
public class SampleController : Controller
{
    [HttpPost("DownloadFile")]
    public void DownloadFile(string strData)
    {
        var cd = new System.Net.Mime.ContentDisposition { 
                     FileName = "test.rar", Inline = false };                
        Response.Headers.Add("Content-Disposition", cd.ToString());
        byte[] arr = System.IO.File.ReadAllBytes("G:\\test.rar");                
        Response.Body.WriteAsync(arr, 0, arr.Length);  

The need is that the control flow must go to the controller, execute some logic, and then send byte [] response content to the client side, the user needs to save the file. No cshtml, just html with jquery ajax call.

+4
3

HttpPost? HttpGet :

public async void DownloadFile()
{
    Response.Headers.Add("content-disposition", "attachment; filename=test.rar");
    byte[] arr = System.IO.File.ReadAllBytes(@"G:\test.rar");
    await Response.Body.WriteAsync(arr, 0, arr.Length);
}

. , FileStreamResult:

[HttpGet]
public FileStreamResult DownloadFile() {
    Response.Headers.Add("content-disposition", "attachment; filename=test.rar");
    return File(new FileStream(@"G:\test.rar", FileMode.Open),
                "application/octet-stream"); // or "application/x-rar-compressed"
}
+8

, . .

public ActionResult DownloadFile(string strData) {
    var cd = new System.Net.Mime.ContentDisposition { FileName = "test.rar", Inline = false };
    byte[] arr = System.IO.File.ReadAllBytes(@"G:\test.rar");

    Response.ContentType = "application/x-rar-compressed";
    Response.AddHeader("content-disposition", cd.ToString());
    Response.Buffer = true;
    Response.Clear();
    Response.BinaryWrite(arr);
    Response.End();

    return new FileStreamResult(Response.OutputStream, Response.ContentType);
}
0

, , Response -, . , , . :

http://forums.asp.net/t/2011115.aspx?How+to+download+a+file+from+a+path+in+asp+net+vnext+

public class FileResult : ActionResult
{
    public FileResult(string fileDownloadName, string filePath, string contentType)
    {            
        FileDownloadName = fileDownloadName;
        FilePath = filePath;
        ContentType = contentType;
    }

    public string ContentType { get; private set; }
    public string FileDownloadName { get; private set; }
    public string FilePath { get; private set; }

    public async override Task ExecuteResultAsync(ActionContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = ContentType;
        context.HttpContext.Response.Headers.Add("Content-Disposition", new[] { "attachment; filename=" + FileDownloadName });
        using (var fileStream = new FileStream(FilePath, FileMode.Open))
        {
            await fileStream.CopyToAsync(context.HttpContext.Response.Body);
        }
    }
}

The main idea is to return an ActionResult and not contact the response directly. As far as I remember, the previous ASP.NET already had some kind of class, but it seems that at the moment it is missing. You just need to return FileResultto your controller.

0
source

Source: https://habr.com/ru/post/1622866/


All Articles