mercredi 1 avril 2020

How to display a PDF file in ASP.NET web app

I have a web api and a web app both written in asp.net and want to send a PDF from the api to the web app for display in a new browser window, the api is working sending the file as a streamcontent but i cannot find a way of prompting the client browser to display this in a new window, i cant make the browser make the request to the api directly as i need to add an authentication header first.

Does anyone have any ideas?

As an alternative if this is not possible i could also have the file downloaded to the client machine but i also dont know how to do that.

api code:

    public HttpResponseMessage Get()
    {
        var response = new HttpResponseMessage();

        string auth;
        try
        {
            auth = Request.Headers.Authorization.Parameter;
        }catch { return new HttpResponseMessage(HttpStatusCode.Unauthorized); }
        if (auth == "test" || auth == null)
        {
            FileStream stream = File.OpenRead(Path.Combine(Storage.FilesDirectory, "0d23b37f-3ade-4342-beea-c6ba2cbf83cb.file"));
            response.Content = new StreamContent(stream);
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
            return response;
        }
        else return new HttpResponseMessage(HttpStatusCode.Forbidden);
    }

Current client code that gets the stream but i dont know how to display that in the browser:

        HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, API.DocumentUrl);
        req.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", "test");
        var res = await HttpClientSingleton.Client.SendAsync(req);



Aucun commentaire:

Enregistrer un commentaire