golang简单获取上传文件大小的实现代码
作者:dotcoo 时间:2024-05-21 10:22:09
本文实例讲述了golang简单获取上传文件大小的方法。分享给大家供大家参考,具体如下:
package main
import (
"fmt"
"io"
"net/http"
"log"
"os"
)
// 获取文件大小的接口
type Size interface {
Size() int64
}
// 获取文件信息的接口
type Stat interface {
Stat() (os.FileInfo, error)
}
// hello world, the web server
func HelloServer(w http.ResponseWriter, r *http.Request) {
if "POST" == r.Method {
file, _, err := r.FormFile("userfile")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if statInterface, ok := file.(Stat); ok {
fileInfo, _ := statInterface.Stat()
fmt.Fprintf(w, "上传文件的大小为: %d", fileInfo.Size())
}
if sizeInterface, ok := file.(Size); ok {
fmt.Fprintf(w, "上传文件的大小为: %d", sizeInterface.Size())
}
return
}
// 上传页面
w.Header().Add("Content-Type", "text/html")
w.WriteHeader(200)
html := `
<form enctype="multipart/form-data" action="/hello" method="POST">
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
`
io.WriteString(w, html)
}
func main() {
http.HandleFunc("/hello", HelloServer)
err := http.ListenAndServe(":12345", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
希望本文所述对大家Go语言程序设计有所帮助。
标签:golang,文件大小


猜你喜欢
520使用Python实现“我爱你”表白
2022-07-16 05:25:47

MySQL中的日期时间类型与格式化方式
2024-01-22 00:10:23

python用moviepy对视频进行简单的处理
2023-08-03 07:02:15

Python 面向切面编程 AOP 及装饰器
2021-05-07 14:16:36

详细聊一聊为什么Python没有main函数
2023-05-13 07:16:29
python实现windows壁纸定期更换功能
2022-09-06 15:05:11

一文教你利用Python制作一个生日提醒
2022-12-12 09:20:43
Python中matplotlib如何改变画图的字体
2023-02-19 16:46:38

浅谈js的html元素的父节点,子节点
2024-04-23 09:31:56
python ElementTree 基本读操作示例
2022-10-23 07:27:25
asp分段插入数据库
2010-07-02 13:13:00
python subprocess 杀掉全部派生的子进程方法
2021-01-28 21:44:28
使用python实现微信小程序自动签到功能
2021-05-30 10:04:11

利用python写个下载teahour音频的小脚本
2021-05-17 06:05:54
python opencv旋转图像(保持图像不被裁减)
2022-02-09 13:56:32

Jupyter notebook 更改文件打开的默认路径操作
2023-02-04 13:59:21

wxpython中利用线程防止假死的实现方法
2022-01-27 00:45:29

一个取图片尺寸的类,支持jpg,gif,png
2007-10-18 10:25:00
python pickle 和 shelve模块的用法
2023-11-07 22:41:11
php巧获服务器端信息
2023-10-04 02:18:39