简体   繁体   中英

How to return a file from web api controller?

I'm using a MVC 5 web Api Controller, and I want to return a file:

[Route("")]
public HttpResponseMessage GetFile()
{
    var statusCode = HttpStatusCode.OK;
    FileStream file = XLGeneration.XLGeneration.getXLFileExigence();

    return Request.CreateResponse(statusCode, file);
}

It dosn't work.

The exception from postman is:

"ExceptionMessage": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'."

I'm posting what worked for me as an alternative in case anybody else is having trouble.

[ActionName("File")]
[HttpGet]
public HttpResponseMessage File()
{
        var response = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new System.IO.FileStream(yourFilePath, System.IO.FileMode.Open);
        response.Content = new StreamContent(stream);
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

        return response;
}

I returned byte[] from WebAPI controller and download PDF successfully. I'm using iTextSharp (LGPL) 4.1.6 free PDF converter. To install iTextSharp (LGPL / MPL), run the following command in the Package Manager Console.

Install-Package iTextSharp-LGPL -Version 4.1.6

Server side code

    [Route("Export/ExportToPdf")]

    public byte[] ExportToPdf(string html)
    {           

        MemoryStream msOutput = new MemoryStream();
        TextReader reader = new StringReader(html);
        Document document = new Document(new Rectangle(842, 595));             
        PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
        document.Open();
        document.HtmlStyleClass = @"<style>*{ font-size: 8pt; font-family:arial;}</style>";

        var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(html), null);
        foreach (var htmlElement in parsedHtmlElements)
        {
            document.Add(htmlElement as IElement);
        }

        document.Close();

        return msOutput.ToArray();
    }

Client Side Code.

//Call this function inside of AJAX success.
function ExportToPDF(data) {

    //base64 To ArrayBuffer
    var binaryString = window.atob(data);
    var binaryLen = binaryString.length;
    var bytes = new Uint8Array(binaryLen);
    for (var i = 0; i < binaryLen; i++) {
        var ascii = binaryString.charCodeAt(i);
        bytes[i] = ascii;
    }
    //-------

    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(new Blob([bytes], { type: 'application/pdf' }));
    link.download = "Report.pdf";
    link.click();   
}

Try this...

[Route("")]
public HttpResponseMessage GetFile()
{
    var result = new HttpResponseMessage(HttpStatusCode.OK);
    try
    {
        var file = XLGeneration.XLGeneration.getXLFileExigence();
        result.Content = new StreamContent(file);
        result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
        var value = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        value.FileName = "Whatever your filename is";
        result.Content.Headers.ContentDisposition = value;
    }
    catch (Exception ex)
    {
        // log your exception details here
        result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }
    return result;
}

This should actually stream it back as a file.

Just idea:

public HttpResponseMessage GetFile()
{
    FileStream file = XLGeneration.XLGeneration.getXLFileExigence();

    using(var sr = new StreamReader(file))
    {
        content = sr.ReadToEnd();

        return new HttpResponseMessage
        {
            Content = new StringContent(content, Encoding.UTF8, "application/json")
        };
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM