Reactjs file downloade pdf corrupted
Already on GitHub? Sign in to your account. I am receiving a pdf file from the server on the internet. Whereas if I simply return the file without a cb, it gets downloaded. I've read on multiple threads of having issues in downloading pdfs in node. Is there any solution that you could recommend which would allow us to use a callback instead of a return statement since that would intercept all middleware.
The text was updated successfully, but these errors were encountered:. Sorry, something went wrong. I don't think strong-remoting recognizes the type 'file'.
Also, what's the originalUrl? Are you using loopback-component-storage? I haven't played around with file transfers for a while, I will try to see if one of the other dev leads has a solid answer for you. Can you ask a developer who has worked on this to get back to us. We could try setting a webex to share screen and debug this to take this further. Hey varunj90 , I appreciate your patience. I did a little bit of a deeper dive and talked to bajtos as well.
Here is the issue: remoteMethods have these rules for options see the options section. The Type of your return object is not a LoopBack Type , in other words type: 'file' is not supported. You could probably use loopback-component-storage to get the file transfer to work. I'm sorry, this is definitely not the response that you were hoping for, but you will have to make a 'feature request' to add that functionality to a regular LoopBack-model.
Do you have any examples in terms of setting this up using a buffer and also using loopback-component-storage? I'm going to reiterate the problem: We are getting a pdf from a backend that we need to send to the client in a callback. Is there any way in loopback to perform this? We aren't sure if we are using the correct return type. Here the module: LoopBack-component-storage Here is a tutorial there is also a link to the example-repo within the doc. As for setting up some time to view your code, we have a dedicated support channel for this purpose: support strongloop.
You can email them to set up a call with our support team. In the above case, I preloaded pdf. You would have to modify my example to accept a docid as well.
Also, I would like to change the title of this issue to be more descriptive and label it as a feature request where you could use the callback: cb null, pdf instead of directly calling res. Zendesk ticket has been linked to this issue and is now the primary ticket. If we simply do a res. I have all the information as part of that thread. I have also attached screenshots of the remote method we are using right now and how it works well without a callback. Question to you is: how to use the callback in a scenario like this?
I see richardpringle has questioned if we need a callback and I've responded why it would be nice to have a callback. Wondering if this is a limitation on loopback that needs a feature update.
I asked another one of our Developers to look a this as well and he just commented on your issue. He pointed out a few code samples that could help deal with file downloads as a remote method, which are located here:. In my opinion, the matter is clear: varunj90 needs a new feature - support for Buffer return arguments. It is important to specify root:true in returns arg metadata, otherwise the result is wrapped in an object, e.
In order to preserve backwards compatibility, I am proposing to add a new flag encoding: 'raw' that would indicate that the Buffer value should be sent as-is. At the moment, we always serialise values to JSON, which is something we should preserve for backwards compatibility. Here is a short overview of different responses depending on returns flags based on my knowledge of the code.
I did not run an example to verify my assumptions, so I may be incorrect in some places:. Seems like you got it spot on. That is pretty much what we need and whats missing as a feature in loopback. Sameer Thite Sameer Thite 1, 2 2 gold badges 6 6 silver badges 11 11 bronze badges. Triggering browser download from front-end is not a reliable way to do it.
You should create endpoint that when called, will provide the correct response headers, thus triggering the browser download. From my understanding, you are saying that it can be achieved by rest api with correct response headers, is it right?
I didn't know how to attach a link in comment, so I posted an answer. Add a comment. Active Oldest Votes. Anchor tag opens image in same browser tab preview of image and then we need to manually save the image. I am trying to achieve automatic download on button click, without opening file preview — Sameer Thite. Refer to this for more details: stackoverflow.
I try for zip file, the file downloads, and never unzips properly. Someone might know the reason for it? Btw, I find a workaround: stackoverflow.
Triggering browser download from front-end is not reliable. Jackyef Jackyef 3, 15 15 silver badges 22 22 bronze badges. What you should do is, create an endpoint that when called, will provide the correct response headers, thus triggering the browser download What are those header and how does specifics headers can trigger browser download?
Brian Li Brian Li 9 9 silver badges 16 16 bronze badges. Mohammad altenji Mohammad altenji 2, 19 19 silver badges 32 32 bronze badges. Except that browser will open a PDF, and I want it to download.
Not open. I have added below link. VnoitKumar 1, 12 12 silver badges 23 23 bronze badges. Kevin Kevin 2 2 silver badges 3 3 bronze badges. I can't open file after download, File error : "The file nameFile could not be opened. It will open pdf in new tab while using fileSaver — Divyesh Kanzariya. DivyeshKanzariya how can you resolve this question? Otabek Butcher Otabek Butcher 6 6 silver badges 9 9 bronze badges.
Be aware that if the page is not reloaded in your app, the blob passed to "URL. For a long-running app, this may lead to wasted memory and perfomance issues. Hi, I know there is already a very similar issue here but it does not provide a solution, because the author of this issue just uses a different approach to download the file in the end. I'm trying to download a file which is generated in the nodejs backend from the angular frontend, but when I try to open it, it's always corrupted. Of course I have seen the example in the readme of this project and tried it, but it also came out corrupted.
I can write the output to a file and then point the browser to it by using window. Sadly I could not get this approach working with my authorization code.
I read somewhere that it's better to send buffer objects when working with expressjs, so here is my code right now:. The text was updated successfully, but these errors were encountered:.
After generating the workbook, can you write to some file on the server side for example, fs. Sorry, something went wrong. I have the same corruption issue when writing my document. I am editing the cells according to the documentation, and my Excel files output can be read, but I get almost every time the corrupt message error.
Here is what i found : if i console log wbout, i get wrong characters that may the cause of the corruption. I think the problem is not the actual writing of the file but the sending to the client. When I open the downloaded file in a texteditor and compare it to the written on the server, I can see that they have different hex codes.
So, after all this might not be an Issue with js-xlsx but with expressjs or what ever. I read alot about disabeling the live-reload component but that did not do the trick for me. Still, I would be very thankful for any help on this matter! I think this is actually an issue in angularJS and has nothing to do with this project. Thank you anyways! Similar issues have come up before. If you don't mind, can you put together a very short and simple example using Angular that we can reference later?