|
| 1 | +// Copyright 2020 beego-dev |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/astaxie/beego" |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + ctrl := &MainController{} |
| 23 | + beego.Router("/download", ctrl, "get:Download") |
| 24 | + beego.Router("/download_file", ctrl, "get:DownloadFile") |
| 25 | + beego.Run() |
| 26 | +} |
| 27 | + |
| 28 | +type MainController struct { |
| 29 | + beego.Controller |
| 30 | +} |
| 31 | + |
| 32 | +func (ctrl *MainController) DownloadFile() { |
| 33 | + // The file LICENSE is under root path. |
| 34 | + // and the downloaded file name is license.txt |
| 35 | + ctrl.Ctx.Output.Download("LICENSE", "license.txt") |
| 36 | +} |
| 37 | + |
| 38 | +// Download is an example that download the content stored in memory |
| 39 | +// when you access localhost:8080/download |
| 40 | +// A file named "mytest.txt" with content "Hello" will be downloaded |
| 41 | +func (ctrl *MainController) Download() { |
| 42 | + output := ctrl.Ctx.Output |
| 43 | + output.Header("Content-Disposition", "attachment;filename=mytest.txt;") |
| 44 | + output.Header("Content-Description", "File Transfer") |
| 45 | + output.Header("Content-Type", "application/octet-stream") |
| 46 | + output.Header("Content-Transfer-Encoding", "binary") |
| 47 | + output.Header("Expires", "0") |
| 48 | + output.Header("Cache-Control", "must-revalidate") |
| 49 | + output.Header("Pragma", "public") |
| 50 | + ctrl.Ctx.WriteString("Hello") |
| 51 | +} |
0 commit comments