0

I am doing a small project in ASP.NET Core (backend) and VueJs (frontend) the communication is by axios.

I want to send a PDF file to a local path on the server. And show it in a new window.

I've been looking for information for a couple of days and I only get more confused. This is the closest I've ever been, and I honestly don't know what I'm doing :(

(I apologize for my English)

VueJS & Vuetify

 this.$proxies.menusProxy
        .getFileStream(
          this.eitem.identificador,
          {
            params: {            
              file: "Clientes.pdf",
            },
          },
        )
        .then((x) => {
          console.log("Done:  ", x);
                
        })
        .catch((x) => {
          console.log("Error:  ", x);
        });
    })
    .catch((x) => {});

ASP.NET

public async Task<HttpResponseMessage> GetFileStream(string file)
    {
            var uploads = Path.Combine("c:\\Shared\\06W03R0821105PLAE1\\"+file);

                
                MemoryStream responseStream = new MemoryStream();
                Stream fileStream = System.IO.File.Open(uploads, FileMode.Open);

                fileStream.CopyTo(responseStream);
                fileStream.Close();
                responseStream.Position = 0;

                HttpResponseMessage response = new HttpResponseMessage();
                response.StatusCode = HttpStatusCode.OK;

                
                response.Content = new StreamContent(responseStream);
                string contentDisposition = string.Concat("attachment; filename=", file);
                response.Content.Headers.ContentDisposition =
                              ContentDispositionHeaderValue.Parse(contentDisposition);
                return response;

    }
        

Console response:

Image Console

Thank you very much to all

  • I'm unable to understand what you want to do. "I want to send a PDF file to a local path on the server. And show it in a new window." - does it mean you want to want to upload it from browser and then fetch it again and show it in a new browser window? Or do you want to load an existing pdf that is already stored on the server? or... ? And where's your GetFileStream() method? – Christoph Lütjen Oct 02 '20 at 14:54
  • @ChristophLütjen I have not put the method calls so as not to fill the screen with more code. And yes, the PDF is local, on the server. By clicking on a button I want to recover that pdf and show it – Brian Flores Oct 02 '20 at 15:02
  • So generally the way you do that is expose an action method that returns the PDF. That action method can take a URL parameter that indicates which PDF should be returned, though you may need to implement access controls if access to certain PDF's is limited to certain users. Then your webpage would have a PDF viewer component, and you point the href to the URL that leads to your action method. – mason Oct 02 '20 at 15:08
  • Your code is using the response message from HttpClient which is the wrong component here. Use HttpClient if you want to fetch data from C# code. You need a simple controller action that returns your pdf as described e.g. here: https://stackoverflow.com/questions/40486431/return-pdf-to-the-browser-using-asp-net-core – Christoph Lütjen Oct 03 '20 at 16:55

0 Answers0