Meteor 前端 RESTful API 通過後端 API 下載檔案,meteorrestful

來源:互聯網
上載者:User

Meteor 前端 RESTful API 通過後端 API 下載檔案,meteorrestful
Meteor 下載檔案問題情境

後端 HTTP 伺服器提供一個下載介面,但是需要前端 Meteor 能夠給瀏覽器使用者開一個URL來下載這個檔案。

舉例:線上的Meteor Logo檔案就好比後端提供的 RESTful API,然後我們給瀏覽器客戶暴露一個 URL 來下載

Meteor 依賴

安裝所有依賴:

meteor add httpmeteor add cfs:http-methodsmeteor add froatsnook:request

說明:
* cfs:http-methods * 用來給Meteor項目提供一個方便建立 RESTful API 的機會,非常方便。
here for details.

* froatsnook:request * 用來給Meteor項目提供一個便利訪問位元據的 RESTful API 的機會,也是非常簡潔方便,支援同步請求。
here for details.

範例程式碼Meteor 伺服器端
if (Meteor.isServer) {  // exports a RESTful API for browser  HTTP.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.      var res = this.createWriteStream();      // Play as a HTTP client for requesting image.      // It is Sync way      var result = request.getSync("https://meteor.com/meteor-logo.png", {        encoding: null      });      var buffer = result.body;      // TODO: need to set header for response here which transfered by      // response of logo request.      res.write(buffer);      res.end();    }  });} // Meteor.isServer enclosure
瀏覽器查看是否有Logo
meteor --port 3000

完

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.