Skip to content

Commit

Permalink
Merge pull request #17 from flycash/ftr/download
Browse files Browse the repository at this point in the history
Add download example
  • Loading branch information
flycash authored Jul 5, 2020
2 parents d25eb38 + 4b0ccca commit eb44b6a
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions httpserver/download/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020 beego-dev
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/astaxie/beego"
)

func main() {
ctrl := &MainController{}
beego.Router("/download", ctrl, "get:Download")
beego.Router("/download_file", ctrl, "get:DownloadFile")
beego.Run()
}

type MainController struct {
beego.Controller
}

func (ctrl *MainController) DownloadFile() {
// The file LICENSE is under root path.
// and the downloaded file name is license.txt
ctrl.Ctx.Output.Download("LICENSE", "license.txt")
}

// Download is an example that download the content stored in memory
// when you access localhost:8080/download
// A file named "mytest.txt" with content "Hello" will be downloaded
func (ctrl *MainController) Download() {
output := ctrl.Ctx.Output
output.Header("Content-Disposition", "attachment;filename=mytest.txt;")
output.Header("Content-Description", "File Transfer")
output.Header("Content-Type", "application/octet-stream")
output.Header("Content-Transfer-Encoding", "binary")
output.Header("Expires", "0")
output.Header("Cache-Control", "must-revalidate")
output.Header("Pragma", "public")
ctrl.Ctx.WriteString("Hello")
}

0 comments on commit eb44b6a

Please sign in to comment.