return File get error in api?

mc 4,596 Reputation points
2022-07-01T04:34:43.063+00:00

What I want is to download a excel from a api

but I get

PK Æ $Vb eO Gâ
XB ¶*¡ .mÿù~ ù|ÿXo'? LÙQPrYÕR`0d]h ¹YÜÊífý æ È Y ì ã @6 z + ÊKCÉk.ÇÔBÔ¦×-ª®o íwÈÃN±³J¦ ]Jñô ñ nj gð Ìè1ð / ¥Y§ YÉi WJý3Q_ R)à4fuI N Á¢]ÄTæ ;Ì¿"Kæ¡\gÐ1Î ®.I2cfòó ï̬éú|Óßû

such code .

return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");  

I am using ajax post to the api.
I can not download the file why?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,612 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JasonPan - MSFT 5,906 Reputation points Microsoft Vendor
    2022-07-01T08:26:46.877+00:00

    Hi @mc

    Please try adding responseType in the ajax request. The value of responseType can be arraybuffer, or blob. Code like:

    $.ajax({  
        url: url,  
        method: 'POST',  
        xhrFields: { responseType: 'arraybuffer'}  
    })  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Jason

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.