A simple example of an HTTP upload and download in Go
Start with
go run main.go
A simple example of an HTTP upload and download in Go
License: MIT License
It's admittedly a common mistake to forget it, File.Close()
can and sometimes does return an error, too, which needs to be handled. (Close()
is idempotent, you can call it multiple times.)
As next step you will need to check if a file has been created (despite the error), and if so then remove it.
(Or more elegantly, create with Linux' O_TMPFILE
. See my caddy.upload.)
In article, it is mentioned that const maxUploadSize = 2 * 1024 // 2 MB
. But actually right value for 2MB is 2 * 1024 * 1024
Also minor typo: there are two variables filetype
and fileType
You know, there are people like me who copy/paste code without reading whole article and directly deploy to prod without testing :)
Hi.
I have the same issue on the Postman. At first, the problem occurred because of uploadFile
key then it has occurred because of the first CANT_WRITE_FILE
statement:
defer newFile.Close()
if _, err := newFile.Write(fileBytes); err != nil || newFile.Close() != nil {
renderError(w, "CANT_WRITE_FILE2", http.StatusInternalServerError)
return
}
Any suggestion?
golang-http-file-upload-download/main.go
Line 14 in 87ee874
最近一直在找 https-server
类型的上传下载服务器
最近不是 golang
比较火吗?
所以,有兴趣 再修个轮子吗?
大致流程:
server:监听并运行http服务
clinet:
当然有推荐的欢迎指出,谢谢,❤️
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.