Commit 9f59fe7c5e248268d85ea7c7b1a2a292a1ea6930

Authored by Administrator
1 parent df7bfe33
Exists in master

upload file ok

controllers/attach.go 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/astaxie/beego"
  5 + "net/url"
  6 + "os"
  7 + "io"
  8 +)
  9 +
  10 +type AttachController struct {
  11 + beego.Controller
  12 +}
  13 +
  14 +func (this *AttachController) Get() {
  15 + //GET /file/TIM%e5%9b%be%e7%89%8720171224234516.jpg
  16 + filePath, err := url.QueryUnescape(this.Ctx.Request.RequestURI[1:])
  17 + if err != nil {
  18 + this.Ctx.WriteString(err.Error())
  19 + return
  20 + }
  21 + //filePath=file/TIM图片20171224234516.jpg
  22 + f, err := os.Open(filePath)
  23 + if err != nil {
  24 + this.Ctx.WriteString(err.Error())
  25 + return
  26 + }
  27 + defer f.Close()
  28 +
  29 + _, err = io.Copy(this.Ctx.ResponseWriter, f)
  30 + if err != nil {
  31 + this.Ctx.WriteString(err.Error())
  32 + return
  33 + }
  34 +}
  35 +
  36 +
  37 +
  38 +
  39 +
... ...
controllers/topic.go
... ... @@ -3,6 +3,7 @@ package controllers
3 3 import (
4 4 "go_blog/models"
5 5 "github.com/astaxie/beego"
  6 + "path"
6 7 )
7 8  
8 9 type TopicController struct {
... ... @@ -33,11 +34,27 @@ func (this *TopicController) Post() {
33 34 content := this.Input().Get("content")
34 35 category := this.Input().Get("category")
35 36  
36   - var err error
  37 + //获取附件
  38 + _, fh, err := this.GetFile("attachment")
  39 + if err != nil {
  40 + beego.Error(err)
  41 + }
  42 +
  43 + var attachment string
  44 + if fh != nil {
  45 + attachment = fh.Filename
  46 + beego.Info(attachment)
  47 + err = this.SaveToFile("attachment", path.Join("file", attachment))
  48 + if err != nil {
  49 + beego.Error(err)
  50 + }
  51 + }
  52 +
  53 +
37 54 if len(tid) == 0 {
38   - err = models.AddTopic(title,category,content)
  55 + err = models.AddTopic(title, category, content, attachment)
39 56 } else {
40   - err = models.ModifyTopic(tid,title,category,content)
  57 + err = models.ModifyTopic(tid, title, category, content, attachment)
41 58 }
42 59  
43 60 if err != nil {
... ...
data/beeblog.db
No preview for this file type
file/TIM图片20171224234516.jpg 0 → 100644

147 KB

models/topic.go
... ... @@ -4,6 +4,8 @@ import (
4 4 "github.com/astaxie/beego/orm"
5 5 "time"
6 6 "strconv"
  7 + "os"
  8 + "path"
7 9 )
8 10  
9 11 func GetAllTopics(isDesc bool) (topics []*Topic, err error) {
... ... @@ -21,12 +23,13 @@ func GetAllTopics(isDesc bool) (topics []*Topic, err error) {
21 23 return
22 24 }
23 25  
24   -func AddTopic(title, category, content string) error {
  26 +func AddTopic(title, category, content, attachment string) error {
25 27 o := orm.NewOrm()
26 28 topic := &Topic{
27 29 Title: title,
28 30 Category: category,
29 31 Content: content,
  32 + Attachment: attachment,
30 33 Created: time.Now(),
31 34 Updated: time.Now(),
32 35 ReplyTime: time.Now(),
... ... @@ -56,7 +59,7 @@ func GetTopic(tid string) (*Topic, error) {
56 59 return topic, err
57 60 }
58 61  
59   -func ModifyTopic(tid, title, category, content string) error {
  62 +func ModifyTopic(tid, title, category, content, attachment string) error {
60 63 tidNum, err := strconv.ParseInt(tid,10,64)
61 64 if err != nil {
62 65 return err
... ... @@ -64,14 +67,27 @@ func ModifyTopic(tid, title, category, content string) error {
64 67  
65 68 topic := &Topic{Id:tidNum}
66 69  
  70 + var oldAttach string
  71 +
67 72 o := orm.NewOrm()
68 73 //如果数据库里有这个文章的id,才操作
69 74 if o.Read(topic) == nil {
  75 + //原有附件放到这个变量
  76 + oldAttach = topic.Attachment
70 77 topic.Title = title
71 78 topic.Category = category
72 79 topic.Content = content
  80 + topic.Attachment = attachment
73 81 topic.Updated = time.Now()
74   - o.Update(topic)
  82 + _, err = o.Update(topic)
  83 + if err != nil {
  84 + return err
  85 + }
  86 + }
  87 +
  88 + //删除旧附件
  89 + if len(oldAttach) > 0 {
  90 + os.Remove(path.Join("file", oldAttach))
75 91 }
76 92  
77 93 return nil
... ...
routers/router.go
... ... @@ -21,4 +21,8 @@ func init() {
21 21 //当往/reply/add路径执行post方法,将执行Delete函数
22 22 beego.Router("/reply/delete", &controllers.ReplyController{},"get:Delete")
23 23  
  24 + //附件作为静态文件,只可访问具体路径,访问目录返回403
  25 + //beego.SetStaticPath("/file", "file")
  26 + //上面的是一种方式,下面是另外一种方式
  27 + beego.Router("/file/:all", &controllers.AttachController{})
24 28 }
... ...
views/topic.html
... ... @@ -15,7 +15,7 @@
15 15 <table class="table table-striped">
16 16 <thead>
17 17 <tr>
18   - <th>#</th>
  18 + <th>编号</th>
19 19 <th>标题</th>
20 20 <th>更新时间</th>
21 21 <th>浏览数</th>
... ...
views/topic_add.html
... ... @@ -11,7 +11,7 @@
11 11  
12 12 <div class="container">
13 13 <h1>添加文章</h1>
14   - <form method="post" action="/topic">
  14 + <form method="post" action="/topic" enctype="multipart/form-data">
15 15 <div class="form-group">
16 16 <label>文章标题:</label>
17 17 <input type="text" class="form-control" name="title">
... ... @@ -26,6 +26,12 @@
26 26 <label>文章内容:</label>
27 27 <textarea name="content" cols="30" rows="10" class="form-control"></textarea>
28 28 </div>
  29 +
  30 + <div class="form-group">
  31 + <label>文章附件:</label>
  32 + <input type="file" class="form-control" name="attachment">
  33 + </div>
  34 +
29 35 <button type="submit" class="btn btn-default">添加文章</button>
30 36 </form>
31 37 </div>
... ...
views/topic_modify.html
... ... @@ -11,7 +11,7 @@
11 11  
12 12 <div class="container">
13 13 <h1>修改文章</h1>
14   - <form method="post" action="/topic">
  14 + <form method="post" action="/topic" enctype="multipart/form-data">
15 15 <input type="hidden" name="tid" value="{{.Topic.Id}}">
16 16 <div class="form-group">
17 17 <label>文章标题:</label>
... ... @@ -27,6 +27,11 @@
27 27 <label>文章内容:</label>
28 28 <textarea name="content" cols="30" rows="10" class="form-control">{{.Topic.Content}}</textarea>
29 29 </div>
  30 +
  31 + <div class="form-group">
  32 + <label>文章附件(<a href="/file/{{.Topic.Attachment}}">{{.Topic.Attachment}}</a>):</label>
  33 + <input type="file" class="form-control" name="attachment">
  34 + </div>
30 35 <button type="submit" class="btn btn-default">修改文章</button>
31 36 </form>
32 37 </div>
... ...
views/topic_view.html
... ... @@ -13,6 +13,7 @@
13 13 {{with .Topic}}
14 14 <h1>{{.Title}}</h1>
15 15 {{.Content}}
  16 + <h5>文章附件:<a href="/file/{{.Attachment}}">{{.Attachment}}</a></h5>
16 17 {{end}}
17 18 </div>
18 19  
... ...