Javascript download url as file react
I'm downloading png files and pdf files. When I open the downloaded image, it says it's corrupted and downloaded pdf shows white background only. How can I download a file correctly? As I am not able to add comments so posting as answer.
I have tried the same thing and posted the question for same in this link. You are getting corrupted file because you are not receiving content as blob or arraybuffer. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
How to download a file through an API in React? Ask Question. Python Show sub menu. Python Answers. Beautiful Soup. Careers Show sub menu. Practice Project Ideas. Reviews Show sub menu. Useful APIs. Books Show sub menu. Book Reviews. Sign Up for My Mailing List. Developer Tools Show sub menu.
Code Formatters Show sub menu. JavaScript Code Beautifier. Crypto Show sub menu. AES Encryptor and Decryptor. MD5 Hash Generator. SHA Hash Generator. SHA Generator. CSV Show sub menu. Content Generators Show sub menu. Canadian Province Array and Select Element. Countries Array and Select Element. Credit Card Number Validator. Dummy Image Generator. Dummy Text Generator. Fake Address Data Generator. Fake Internet Data Generator.
Fake Person Data Generator. 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. Active 2 months ago. Viewed k times.
I know it'd be better to do it serverside, but this project is just for a bit of fun. My issue is downloading the files at the end. Can anyone think of another solution or some kind of workaround? Syscall Mikee Mikee 2, 2 2 gold badges 16 16 silver badges 16 16 bronze badges. Current support is, unfortunately, rather limited — Casebash. Yes, indeed, in is so. Add a comment. Active Oldest Votes. You can simplify that with link. Nice solution. The variable 'link' goes out of the scope at the end of the function note we never added it to the dom so will be garbage collected shortly after.
This only works in Chrome. I tried with download. Show 8 more comments. Zibri Zibri 7, 2 2 gold badges 43 43 silver badges 39 39 bronze badges. Now they both work. This works in most modern browsers, but I'd note that appending to the document then removing is necessary to support some older browsers. Your second solution should be used whenever the dataUri becomes too big depends on the browser, but Chrome does not accept Uri of multiple megabytes in my experience.
See also stackoverflow. Pekka Pekka k gold badges silver badges bronze badges. The new download location seems to be github. If you can't use Flash but are running a Java server-side component, you can use this: github. It uses a Servlet to 'bounce' the data from client.
0コメント