Today I had an interesting challenge. I had to download a file in javascript. Why through javascript and not through a link or a form post?
- I had to set a custom accept header to get the file into a specific format (in my case in excel format)
- I had to pass too many parameters to fit in a querystring.
So here’s the code i ended up using:
export function downloadFile(url: string, message: any, method: string, contentType ? : string, acceptContentType ? : string) { var xhr = new XMLHttpRequest(); xhr.open(method, url); if (acceptContentType) { xhr.setRequestHeader('Accept', acceptContentType); } if (contentType) { xhr.setRequestHeader('content-type', contentType); } xhr.responseType = 'blob'; xhr.onload = function() { if (this.status >= 400) { alert('error') } else { if (window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(xhr.response, 'afile.xlsx'); } else { var objectUrl = URL.createObjectURL(xhr.response); window.location.href = objectUrl; } } }; xhr.send(message); }
I tried to do this via jquery.ajax but I couldn’t get it to work. So then I switched to using xmlhttprequest. This worked fine, though saving a file in javascript works differently in chrome / explorer.
This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this.
ReplyDeleteDot Net Training Institute in Chennai
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru JavaScript Online Training India. Nowadays JavaScript has tons of job opportunities on various vertical industry.
DeleteHi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from JQuery Training
Great Article
DeleteProject Centers in Chennai
Final Year Project Domains for CSE
JavaScript Training in Chennai
JavaScript Training in Chennai
Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
ReplyDeleteRegards,
SAP Training in Chennai with placement | java training in chennai with placement
Tiếng anh cho người đi làm
ReplyDeleteTiếng anh cấp tốc
Luyện thi toeic tại hà nội
Tiếng anh cho người lớn tuổi
Tiếng anh cho người mới bắt đầu
Học tiếng anh tại hà nội
dạy tiếng anh cho doanh nghiệp
Thanks for sharing
ReplyDeletered hat linux training in chennai | rhce training in chennai | linux training in chennai
This comment has been removed by the author.
ReplyDeleteI been working as a Web Designer for 5+ years, as a experienced guy this post also helps me to learn something new about the subject, Very well explanation and example. Thanks for it., Also I'm having a website for AngularJS Training which is one of the powerful tool.
ReplyDeleteTo add onto that JavaScript is an object oriented language. However the way JavaScript handles objects and inheritance is a bit different from conventional object oriented programming languages like Java.
ReplyDeleteweb design lessons
Your Blog is really amazing with smart and interesting content.. Thanks for posting this wonderful post.It is very helpful to everyone..
ReplyDeleteNo.1 Embedded Project Center in Chennai | No.1 MatLab Project Center in Chennai | No.1 Application Project Center in Chennai
Problems mainly occur when the programs are downloaded from unrealizable sources. Therefore, the most crucial thing is to choose a trusted website that only offers hand picked quality software. Ocean Of Games
ReplyDeleteThe whole process is very awesomely automated and easy. The driver updating program doesn't just stop at scanning your entire computer to detect any driver hat is malfunctioning but will also advise you on certain drivers that your system requires. Get Into Pc
ReplyDeleteGood, I am only beginner in Java and your article very helped me. Usually I learn Java on this service with java foreach https://explainjava.com/for-each-loop-java/ that always helps me to learn something new, interesting and most of all useful for me.
ReplyDeleteThat is why selling advertising campaigns marketing so that you could invaluable explore previous advertisment. Quite simply to jot down stronger set that fit this description. Getinto Pc
ReplyDeletePretty article! I found some useful information in your blog Satta King
ReplyDeleteI am only beginner in Java and your article very helped me.
ReplyDeleteigg games
There are so many useful information in your post, I like it all the time. And I want to share this boost app ranking with you, you can take a look. This is very useful.
ReplyDeleteThanks for sharing
ReplyDeletefilehippo
It was nice unblock game . Very informative and expressive .things are easily play online at
ReplyDeleteunblocked games 66
I am really impress with you for the selecting of new and unique topic and also well written article on it. Thanks for sharing with us.
ReplyDeletefilehippo
file hippo
File hippo
Download Free PC Software
File hippo
getintopc for best pc software you can get here
ReplyDelete
ReplyDeleteocean of games is best site for download games
oceans of games is really best games download site for pc Ocean Games
ReplyDeletePretty article! I found some useful information in your blog, it was awesome to read..Mat Lab Project Center in Chennai | Mat Lab Project Center in Velachery
ReplyDeletefree download full version pc game at ovagames
ReplyDeleteThanks for posting this useful content, Good to know about new things here..PHP Project Center in Chennai | PHP Project Center in Velachery
ReplyDeleteThanks for sharing such an interesting and informative article.keep updating..
ReplyDeleteFinal Year Project Center in Chennai | Final Year Project Center in Velachery
try if doest work propely
ReplyDeleteMoviebox Not Working 2018
download
MOVIEBOX FOR PC 2018
try this tool to crack your pattern lock
Android Multi Tools 2018
Thanks on your marvelous posting! I certainly enjoyed reading it, you happen to be a great author.I will ensure that I bookmark your blog and will often come back in the foreseeable future....
ReplyDeleteGood Summer Courses for Hardware Networking | Best Summer Course in Chennai
Great explanation to given on this post..The given information very impressed for me really so nice content.Technical Workshop in Chennai | Summer Courses in Chennai
ReplyDeleteNice Blog Post.Thanks For Sharing It.
ReplyDeleteMost of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Get into pc
ReplyDeletethat's an amazing blog
ReplyDeleteExtraordinary blog. you put Good stuff. All the themes were clarified briefly.Thanks for sharing that post.Keep in blogging.
ReplyDeleteDigital Marketing Training Institute in Chennai | SEO Training in Chennai
There are cases where sites are specifically set up to scam people and the last thing you want is to download a game from such a site. Free Slope Unblocked at School
ReplyDeleteThanks again for the article. Want more. software
ReplyDeleteNice article. Was searching for this code for long time. Thanks for sharing.
ReplyDeleteCheers,
http://www.flowerbrackets.com/iterate-over-array-using-foreach-loop-in-java/
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. Do check Six Sigma Training in Bangalore | Six Sigma Training in Dubai & Get trained by an expert who will enrich you with the latest trends.
ReplyDeleteGood job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
ReplyDeleteBlue Prism Training in Chennai
Blue Prism Training Chennai
Blue Prism Training in Tambaram
Blue Prism Training in Velachery
Blue Prism Training in OMR
Blue Prism Training in TNagar
This was really one of the best blog i have read recently. Thanks for sharing this post with us.
ReplyDeleteIELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Mumbai
Best IELTS Coaching in Mumbai
IELTS Center in Mumbai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Training in Chennai
IELTS Chennai
Best IELTS Coaching in Chennai
Nice blog!! I hope you will share more info like this. I will use this for my studies and research.
ReplyDeleteAngularjs Training in Chennai
Angularjs Course in Chennai
CCNA Training in Chennai
Salesforce Training in Chennai
Angularjs Training
ui ux design course in chennai
Angularjs Training Institute in Chennai
Angular2 Training in Chennai
Angularjs Training in Chennai
Angularjs Course in Chennai
Great Article
ReplyDeleteFInal Year Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai
Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way. T hank you so much.
ReplyDeleteIELTS Coaching in Coimbatore
IELTS Classes in Coimbatore
German Language course in Coimbatore
best IELTS Coaching in Coimbatore
Best Spoken English Classes in Coimbatore
Digital Marketing classes in coimbatore
Embedded course in Coimbatore
Java course in coimbatore
IELTS Coaching in Coimbatore
Thanks for sharing an informative blog keep rocking bring more details
ReplyDeletemobile application development training online
mobile app development course
mobile application development training
mobile app development course online
mobile application development course
online mobile application development
learn mobile application development
Nice information want to know more about Selenium Training In Chennai Selenium Training In ChennaiData Science Training In Chennai
ReplyDeleteMachine Learning Course In Chennai
Data Science Course In Chennai
Protractor Training in Chennai
jmeter training in chennai
Rpa Training Chennai
Very Informative...Glad to find your blog...Keep Sharing...
ReplyDeleteTESTING & TRAINING ON SELENIUM
ORACLE TRAINING IN CHENNAI
PYTHON TRAINING IN CHENNAI
DATA SCIENCE TRAINING IN CHENNAI
Nice Blog Post.Thanks For Sharing It...
ReplyDeleteSEO Training in Bangalore | SEO Course Fees | Best SEO Classes - Join SEO Training in Bangalore at i Digital Academy and learn how to increase your website Traffic by
Google Certified Trainers with 100% Placements and affordable course fees. Book a Free Demo!
Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
ReplyDeleteSEO Training in Bangalore
SEO Course in Bangalore
SEO Training Institute in Bangalore
Best SEO Training Institute in Bangalore
SEO Training Bangalore
SEO Training in Madurai
SEO Training in Coimbatore
SEO Course in Coimbatore
SEO Training Courses in Coimbatore
Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
ReplyDeleteSEO Training in Chennai
SEO Course in Chennai
SEO Training Institute in Chennai
SEO Classes in Chennai
Best SEO Training in Chennai
Excellent blog, this blog gives more useful information, waiting for more updates
ReplyDeleteDevOps Training in Chennai
DevOps Training in Bangalore
Best DevOps Training in Bangalore
DevOps Course in Bangalore
DevOps Training Bangalore
DevOps Training Institutes in Bangalore
DevOps Training in Marathahalli
AWS Training in Bangalore
Data Science Courses in Bangalore
PHP Training in Bangalore
Thanks for this wonderful blog it is really informative to all.keep update more information about this
ReplyDeleteTally Course in Chennai
Tally Course in Hyderabad
Tally training in coimbatore
Tally course in madurai
Best tally training institute in bangalore
tally course in marathahalli
tally Training in bangalore
Tally Classes in Bangalore
Ethical hacking course in bangalore
Software Testing course in Bangalore
I appreciate you for this blog. More informative, thanks for sharing with us.
ReplyDeleteSalesforce Training in Chennai
salesforce training in bangalore
Salesforce Course in Bangalore
best salesforce training in bangalore
salesforce institute in bangalore
salesforce developer training in bangalore
Python Training in Bangalore
Angularjs Training in Bangalore
salesforce training in marathahalli
salesforce institutes in marathahalli
More valuable post!!! Thanks for sharing this great post with us.
ReplyDeleteJAVA Training in Chennai
JAVA Course in Chennai
advanced java training in chennai
Java training institute in chennai
Java classes in chennai
java training in porur
java training in OMR
Big data training in chennai
Selenium Training in Chennai
IOS Training in Chennai
ReplyDeleteAmazing Post. keep update more information.
Online IELTS Coaching
Online IELTS Classes
IELTS Online course
Best IELTS online coaching
Best IELTS course online
Online IELTS coaching classes
IELTS Exam Online Coaching
IELTS Exam Online Coaching
Top IELTS online Coaching
wonderful content with useful information.
ReplyDeletedigital marketing course in adyar
digital marketing course in vadapalani
digital marketing course in porur
digital marketing course in nungambakkam
Thanks for sharing a valuable topic in this Blog Really it is very helpful and interesting. All the best and do share such articles in future also.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
Thanks for this blog, I'am very much delighted to say that this blog has helped me a lot in gain some extra knowledge.
ReplyDeleteSelenium Training in chennai | Selenium Training in anna nagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
Thanks for taking time to share this post. It is really useful. Continue sharing more like this.
ReplyDeleteOracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore
Had a wonderful time while reading your article. Thanks for sharing,
ReplyDeleteAngular JS Training in Chennai | Certification | Online Training Course | Angular JS Training in Bangalore | Certification | Online Training Course | Angular JS Training in Hyderabad | Certification | Online Training Course | Angular JS Training in Coimbatore | Certification | Online Training Course | Angular JS Training | Certification | Angular JS Online Training Course
Learned a lot of new things in this post. Thanks for taking the time to share this blog...
ReplyDeletewhat does a devops engineer do
what is soft skill development
how to learn tableau
best way to improve spoken english
blue prism technical interview questions
blue prism interview questions for freshers