示例#1
0
//adapted from Revel framework BinaryResult.Apply method
func (r *StreamResult) Write(ctx framework.WebContext) error {

	var err error

	// If we have a ReadSeeker, delegate to http.ServeContent
	if rs, ok := r.Reader.(io.ReadSeeker); ok {
		http.ServeContent(ctx, ctx.Req(), r.Name, r.ModTime, rs)

	} else {
		// Else, do a simple io.Copy.

		if r.Length != -1 {
			ctx.Header().Set("Content-Length", strconv.FormatInt(r.Length, 10))
		}

		ctx.WriteHeader(http.StatusOK)
		//TODO: use a mime-type to file extension strategy like Revel does
		//ctx.ContentType("application/octet-stream")
		//ctx.ContentType(ContentTypeByFilename(r.Name))
		_, err = io.Copy(ctx, r.Reader)
	}

	// Close the Reader if we can
	if v, ok := r.Reader.(io.Closer); ok {
		v.Close()
	}

	return err
}