美文网首页程序员
78.上传文件及在服务器保存文件到任意路径

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

作者: 厚土火焱 | 来源:发表于2019-04-01 23:11 被阅读0次

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

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>企业上传文件</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">
            /*同步数据*/
            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();
            }
        </script>
    </head>
    <body>
    <div>企业上传文件,按照“平台》企业编号》系统》年月日”来储存文件,文件名使用生成时间</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>
    

    实现上传功能的函数

    /**
    * 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 盘了。
    在此基础上,你应该可以动态的指定每次上传文件的物理路径了。

    相关文章

      网友评论

        本文标题:78.上传文件及在服务器保存文件到任意路径

        本文链接:https://www.haomeiwen.com/subject/kwkibqtx.html