Meteor front-end RESTful API downloads files via backend API

Source: Internet
Author: User

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
    1. First you have to start the Meteor service.
3000
    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.