大家好你們好!在本教程中,我們將構(gòu)建一個非常簡單的文件上傳 HTTP 服務(wù)器,它允許您將文件上傳到運行 Go 應(yīng)用程序的服務(wù)器 。
您想要這樣做的原因有無數(shù)種,您可以上傳 CSV 報告以在復(fù)雜的財務(wù)系統(tǒng)中進行進一步處理,或者您可以創(chuàng)建一個很酷的圖像處理應(yīng)用程序,允許您修改您想要的任何照片的各個方面上傳 。
值得慶幸的是 , 在 Go 中處理圖像上傳的任務(wù)相當(dāng)簡單,再加上TempFileGo 1.11 版中引入的新API , 我們可以相當(dāng)快地提出一個非常優(yōu)雅的系統(tǒng)!
步驟我們將從使用該net/http 包創(chuàng)建一個非常簡單的 HTTP 服務(wù)器開始 。這將僅具有一個單獨的端點,它將成為我們的 /upload端點 。
// main.gopackage mainimport ("net/http""fmt")func uploadFile(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "Uploading File")}func setupRoutes() {http.HandleFunc("/upload", uploadFile)http.ListenAndServe(":8080", nil)}func main() {fmt.Println("hello World")}如果我們想運行它 , 我們可以通過運行來實現(xiàn)go run main.go,如果我們沒有犯任何錯誤,我們應(yīng)該看到我們的服務(wù)器啟動成功 。
好的 , 現(xiàn)在我們有了一個可以構(gòu)建的基?。?讓我們開始實現(xiàn)我們的上傳端點來處理文件上傳 。
package mainimport ("fmt""io/ioutil""net/http")func uploadFile(w http.ResponseWriter, r *http.Request) {fmt.Println("File Upload Endpoint Hit")// Parse our multipart form, 10 << 20 specifies a maximum// upload of 10 MB files.r.ParseMultipartForm(10 << 20)// FormFile returns the first file for the given key `myFile`// it also returns the FileHeader so we can get the Filename,// the Header and the size of the filefile, handler, err := r.FormFile("myFile")if err != nil {fmt.Println("Error Retrieving the File")fmt.Println(err)return}defer file.Close()fmt.Printf("Uploaded File: %+v\n", handler.Filename)fmt.Printf("File Size: %+v\n", handler.Size)fmt.Printf("MIME Header: %+v\n", handler.Header)// Create a temporary file within our temp-images directory that follows// a particular naming patterntempFile, err := ioutil.TempFile("temp-images", "upload-*.png")if err != nil {fmt.Println(err)}defer tempFile.Close()// read all of the contents of our uploaded file into a// byte arrayfileBytes, err := ioutil.ReadAll(file)if err != nil {fmt.Println(err)}// write this byte array to our temporary filetempFile.Write(fileBytes)// return that we have successfully uploaded our file!fmt.Fprintf(w, "Successfully Uploaded File\n")}func setupRoutes() {http.HandleFunc("/upload", uploadFile)http.ListenAndServe(":8080", nil)}func main() {fmt.Println("Hello World")setupRoutes()}太棒了 , 我們可以嘗試運行它,并通過go run main.go在我們的終端中再次調(diào)用來查看其他一切是否正常 。
前端我們需要一個非常簡單的 HTML 前端來充當(dāng)我們上傳文件的門戶 。我們不會打擾任何更復(fù)雜的方面,例如身份驗證和用戶管理 , 我們將只創(chuàng)建一個非常簡單的form 元素,它允許我們從本地機器中選擇一個文件并訪問我們上面定義的 API 端點!
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta http-equiv="X-UA-Compatible" content="ie=edge" /><title>Document</title></head><body><formenctype="multipart/form-data"action="http://localhost:8080/upload"method="post"><input type="file" name="myFile" /><input type="submit" value="http://www.wokk.cn/upload" /></form></body></html>太棒了,我們現(xiàn)在可以測試我們所做的工作并且它成功上傳了我們的文件!
嘗試index.html在瀏覽器中打開此文件 , 然后嘗試將文件上傳到我們正在運行的 Web 服務(wù)器 。
您應(yīng)該看到在temp-images/ 遵循約定的目錄中生成了一個新文件upload-23421432.png 。
結(jié)論【前端實現(xiàn)文件上傳功能講解 golang文件上傳需要前端嗎】希望您發(fā)現(xiàn)本教程有用且有趣!如果您這樣做了,或者您發(fā)現(xiàn)本教程有任何問題,請隨時通過下面的建議部分告訴我!
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- Scrivener設(shè)置自動備份為ZIP文件
- 如何在C語言中實現(xiàn)文件內(nèi)容讀取
- Photoshop CC 2017軟件打開及新建文件
- JavaScript在HTML文件中的三個編寫位置
- 實現(xiàn)連桿的往復(fù)運動
- WIN7系統(tǒng)如何查看隱藏的文件和文件夾
- 提升文件安全性的WinRAR小技巧
- 如何快速將DWG文件轉(zhuǎn)為DXF文件
- Excel技巧:輕松實現(xiàn)上下單元格內(nèi)容交換
- 如何在UltraCompare中開啟Word文本文件
