日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html 保存文件指定路径,78.上传文件及在服务器保存文件到任意路径

發布時間:2023/12/4 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html 保存文件指定路径,78.上传文件及在服务器保存文件到任意路径 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上傳文件到服務器是一個常用的操作,而在服務器上保存文件就需要多多用心了。因為你不可能只在一個路徑里保存文件,所以需要實踐一下保存文件到任意位置。當然,前提是你的應用程序有這樣的操作權限。

首先建立一個main.go文件,作為項目的起點。并使用一個網頁模板JoelUploadFile.html,作為操作界面。

代碼目錄結構

在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

企業上傳文件

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;

}

/*同步數據*/

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";

}

}

/*保存修改數據*/

function saveF() {

var frm = document.getElementById("formSave");

var u_id = document.getElementById("u_Id");

if (u_id.value.length <= 0) {

alert('\'流水號\'丟失!');

return;

}

frm.submit();

}

企業上傳文件,按照“平臺》企業編號》系統》年月日”來儲存文件,文件名使用生成時間

style="width: 500px; display: none;" readonly>

上傳附件

style="cursor: pointer"

οnchange="synkFile('uploadfile','u_ElectranicAttachment','new_ElectranicAttachment')">

style="display: none">

實現上傳功能的函數

/**

* 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)) //返回客端數據

}

}

運行此程序

服務器顯示

瀏覽器顯示

點擊“上傳附件”,選擇要上傳的文件。

選擇一個圖片文件

點擊“保存”按鈕,激發js的saveF()函數,提交form到服務器

一般都會順帶提交一些其他數據,例如:u_Id

提交后,文件保存到 "/tmp/" 路徑,這個路徑在當前程序的根上。

已上傳到服務器的文件

服務器返回的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 盤了。

在此基礎上,你應該可以動態的指定每次上傳文件的物理路徑了。

總結

以上是生活随笔為你收集整理的html 保存文件指定路径,78.上传文件及在服务器保存文件到任意路径的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。