上傳文件到服務(wù)器是一個常用的操作,而在服務(wù)器上保存文件就需要多多用心了。因為你不可能只在一個路徑里保存文件她倘,所以需要實踐一下保存文件到任意位置。當然作箍,前提是你的應(yīng)用程序有這樣的操作權(quán)限硬梁。
首先建立一個main.go文件,作為項目的起點胞得。并使用一個網(wǎng)頁模板JoelUploadFile.html荧止,作為操作界面。
在main文件中阶剑,準備好頁面路徑跃巡、上傳路徑、文件訪問路徑等个扰,及相對應(yīng)函數(shù)
/**
* 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("發(fā)生了錯誤瓷炮!")
fmt.Fprintln(writer, err)
}
t.ExecuteTemplate(writer, "JoelUploadFile.html", "")
//---------綁定模板頁 end---------------
}
需要綁定的模板文件 JoelUploadFile.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>企業(yè)上傳文件</title>
<style type="text/css">
input{
line-height: 20px;
padding-left: 2px;
padding-right: 2px;
margin-left: 2px;
margin-right: 2px;
background-color: #eaf4fc;
border-color: #b5c1e4;
border-width: 1px;
border-style: solid;
border-radius: 3px;
}
/*上傳文件*/
.file {
position: relative;
display: inline-block;
background: #D0EEFF;
border: 1px solid #99D3F5;
border-radius: 4px;
padding: 4px 12px;
overflow: hidden;
color: #1E88C7;
text-decoration: none;
text-indent: 0;
line-height: 20px;
}
.file input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
}
.file:hover {
background: #AADFFD;
border-color: #78C3F3;
color: #004974;
text-decoration: none;
}
</style>
<script type="text/javascript">
/*同步數(shù)據(jù)*/
function synkFile(srcObj, showObj, newObj) {
var srcO = document.getElementById(srcObj);
var showO = document.getElementById(showObj);
var new0 = document.getElementById(newObj);
/*if (srcO.value.length > 0) {
showO.value = srcO.value;
}
*/
// showO.value = "";
new0.innerText = "";
for (var i = 0; i < srcO.files.length; i++) {
// showO.value += srcO.files[i].name + "|";
new0.innerText += srcO.files[i].name + "|\r\n";
}
}
/*保存修改數(shù)據(jù)*/
function saveF() {
var frm = document.getElementById("formSave");
var u_id = document.getElementById("u_Id");
if (u_id.value.length <= 0) {
alert('\'流水號\'丟失!');
return;
}
frm.submit();
}
</script>
</head>
<body>
<div>企業(yè)上傳文件递宅,按照“平臺》企業(yè)編號》系統(tǒng)》年月日”來儲存文件娘香,文件名使用生成時間</div>
<div>
<form id="formSave" enctype="multipart/form-data" action="/fileSys/tmpUpload/" method="post">
<input id="u_Id" name="u_Id" type="text" value="joel"
style="width: 500px; display: none;" readonly>
<a href="javascript:;" class="file">上傳附件<input type="file" id="uploadfile" name="uploadfile" multiple
style="cursor: pointer"
onchange="synkFile('uploadfile','u_ElectranicAttachment','new_ElectranicAttachment')"></a>
<label id="show_ElectranicAttachment"></label>
<label id="new_ElectranicAttachment"></label>
<input id="u_ElectranicAttachment" name="u_ElectranicAttachment" type="text" value=""
style="display: none">
<div><input type="button" value="保存" onclick="saveF()"></div>
</form>
</div>
</body>
</html>
實現(xiàn)上傳功能的函數(shù)
/**
* CofoxS
* @Author: Jian Junbo
* @Email: junbojian@qq.com
* @Create: 2019/3/31 23:27
* Copyright (c) 2019 Jian Junbo All rights reserved.
*
* Description: 企業(yè)文件管理
*/
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) {
//接收數(shù)據(jù)
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) //服務(wù)端輸出
resultValue := model.JoelBaseAskResultNormal{}
resultValue.AskResult = tmpfile
back,_ := json.Marshal(resultValue)
fmt.Fprint(writer,string(back)) //返回客端數(shù)據(jù)
}
}
運行此程序
點擊“上傳附件”办龄,選擇要上傳的文件烘绽。
點擊“保存”按鈕,激發(fā)js的saveF()函數(shù)俐填,提交form到服務(wù)器
提交后,文件保存到 "/tmp/" 路徑英融,這個路徑在當前程序的根上盏檐。
下面可以指定保存的其他路徑
當前文件保存的位置是 tmp 路徑歇式,是在 \uilFileSys\EntFileMS.go 中定義的
//create destination file making sure the path is writeable.
dst, err := os.Create("./tmp/" + filename)
用windows系統(tǒng)來舉例,如果我希望文件保存在另一個位置胡野,比如: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:\\"))))
至此熊响,大功告成旨别!你已經(jīng)可以把文件上傳到 e 盤了。
在此基礎(chǔ)上汗茄,你應(yīng)該可以動態(tài)的指定每次上傳文件的物理路徑了秸弛。