文件上传服务器保存形式,78.上传文件及在服务器保存文件到任意路径

上传文件到服务器是一个常用的操作,而在服务器上保存文件就需要多多用心了。因为你不可能只在一个路径里保存文件,所以需要实践一下保存文件到任意位置。当然,前提是你的应用程序有这样的操作权限。

首先建立一个main.go文件,作为项目的起点。并使用一个网页模板JoelUploadFile.html,作为操作界面。

6d5e7be89c6432d73cf654ce685d2cee.png

代码目录结构

在main文件中,准备好页面路径、上传路径、文件访问路径等,及相对应函数

/**

* CofoxS

* @Author: Jian Junbo

* @Email: junbojian@qq.com

* @Create: 2019/3/30 22:25

* Copyright (c) 2019 Jian Junbo All rights reserved.

*

* Description:

*/

package main

import (

"fmt"

"goHttps/uilFileSys"

"html/template"

"log"

"net/http"

)

func main() {

fmt.Println("Hello Moon!")

log.Println("随意指定文件保存路径!")

//--页面路径

http.HandleFunc("/", IndexHandler)

//--上传文件

http.HandleFunc("/fileSys/tmpUpload/", uilFileSys.FileTmpUpload)

//----上传文件

http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("tmp"))))

websitename := ":90"

http.ListenAndServe(websitename, nil)

}

func IndexHandler(writer http.ResponseWriter, request *http.Request) {

//取消获取facicon.ico的访问

if request.RequestURI == "/facicon.ico" {

return

}

//---------绑定模板页 begin-------------

t, err := template.ParseFiles("./templatefile/JoelUploadFile.html")

if err != nil {

fmt.Println("发生了错误!")

fmt.Fprintln(writer, err)

}

t.ExecuteTemplate(writer, "JoelUploadFile.html", "")

//---------绑定模板页 end---------------

}

需要绑定的模板文件 JoelUploadFile.html

企业上传文件

企业上传文件,按照“平台》企业编号》系统》年月日”来储存文件,文件名使用生成时间

实现上传功能的函数

/**

* CofoxS

* @Author: Jian Junbo

* @Email: junbojian@qq.com

* @Create: 2019/3/31 23:27

* Copyright (c) 2019 Jian Junbo All rights reserved.

*

* Description: 企业文件管理

*/

package uilFileSys

import (

"cofoxWebPlatform/platform/lib"

"cofoxWebPlatform/platform/model"

"encoding/json"

"fmt"

"io"

"net/http"

"os"

"path"

)

//上传文件到临时文件路径,前提条件是当前用户是在线用户

func FileTmpUpload(writer http.ResponseWriter, request *http.Request) {

//接收数据

keyId := request.FormValue("keyId") //key身份Id

//get a ref to the parsed multipart form

m := request.MultipartForm

//get the *fileheaders

files := m.File["uploadfile"]

for i,_ := range files {

//save the file as filename

filename := lib.JoelGetNowFullTimeNumber() + path.Ext(files[i].Filename)

//for each fileheader, get a handle to the actual file

file, err := files[i].Open()

defer file.Close()

if err != nil {

http.Error(writer, err.Error(), http.StatusInternalServerError)

return

}

//create destination file making sure the path is writeable.

dst, err := os.Create("./tmp/" + filename)

defer dst.Close()

if err != nil {

http.Error(writer, err.Error(), http.StatusInternalServerError)

return

}

//copy the uploaded file to the destination file

if _, err := io.Copy(dst, file); err != nil {

http.Error(writer, err.Error(), http.StatusInternalServerError)

return

}

tmpfile := dst.Name() // 写入保存文件的位置和文件名

lib.JoelLog(keyId,tmpfile) //服务端输出

resultValue := model.JoelBaseAskResultNormal{}

resultValue.AskResult = tmpfile

back,_ := json.Marshal(resultValue)

fmt.Fprint(writer,string(back)) //返回客端数据

}

}

运行此程序

0b3efcbcc1f64c038ec07876d7155004.png

服务器显示

85324a848ac2a386f3d98137a0f4a395.png

浏览器显示

点击“上传附件”,选择要上传的文件。

495d8dbc048b24078db5d5a8a4e87d10.png

选择一个图片文件

点击“保存”按钮,激发js的saveF()函数,提交form到服务器

e094a5ec68866a1702fa8beed5b5fbde.png

一般都会顺带提交一些其他数据,例如:u_Id

提交后,文件保存到 "/tmp/" 路径,这个路径在当前程序的根上。

2ed611e311cb5af0dc65b301e95c7df4.png

已上传到服务器的文件

2d1e329cb6b4da19829d2424e34f682b.png

服务器返回的json串

下面可以指定保存的其他路径

当前文件保存的位置是 tmp 路径,是在 \uilFileSys\EntFileMS.go 中定义的

//create destination file making sure the path is writeable.

dst, err := os.Create("./tmp/" + filename)

用windows系统来举例,如果我希望文件保存在另一个位置,比如:e盘,这行代码只需要修改为

//create destination file making sure the path is writeable.

dst, err := os.Create("e://" + filename)

如果,你上传之后,还想能够通过浏览器访问到这个文件,那么在 main.go 中,有这样的一行代码

//----上传文件

http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("tmp"))))

你需要把它改成

//----上传文件

http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("e:\\"))))

至此,大功告成!你已经可以把文件上传到 e 盘了。

在此基础上,你应该可以动态的指定每次上传文件的物理路径了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值