Meteor Download File problem scenario
The backend HTTP server provides a download interface, but requires a front-end Meteor to open a URL for the browser user to download the file.
Example: The online Meteor logo file is like a RESTful API provided by the backend, and then we expose a URL to the browser client to download
Meteor Dependency
Install all dependencies:
meteor add httpmeteor add cfs:http-methodsmeteor add froatsnook:request
Description
* Cfs:http-methods * is handy for providing a convenient opportunity to create RESTful APIs for meteor projects.
Here for details.
* Froatsnook:request * provides an opportunity for the Meteor project to provide a RESTful API that facilitates access to binary data, and is also very simple and convenient to support synchronization requests.
Here for details.
Sample code meteor server-side
if(Meteor.isserver) {//Exports a RESTful API for browserHttp.methods ({//Name RESTful API as "Get/download-meteor-logo" '/download-meteor-logo ': function() { //A file in streaming, so need to response to browser as A streaming. varres = This. Createwritestream ();//Play as a HTTP client for requesting image. //It is Sync the varresult = Request.getsync ("Https://meteor.com/meteor-logo.png", {encoding:NULL});varbuffer = Result.body;//Todo:need to set header for response here which transfered by //response of the logo request.Res.write (buffer); Res.end (); } });}//Meteor.isserver Enclosure
Browser to see if there is a logo
- First you have to start the Meteor service.
3000
- Open Browser Access Http://localhost:3000/download-meteor-logo
Finish
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Meteor front-end RESTful API downloads files via backend API