Golang Multipart File Upload Example
http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/
The Go language is one of my favorite programming languages. However, sometimes doing simple things can seem a bit harder than it should. However, most of the time, the problem is just to find out how to do things the easy way. While Go’s documention isn’t
bad, the real key to finding out how to do things is often to look at the source code and
the test suite.
I’m not yet super familiar with all the std lib packages, so when I wanted to test my Go web services, I wrote a few lines of code to create a multipart file upload function that was building the body from scratch. Once I was done messing with the various headers,
boundary protocol etc.. I started testing some edge cases, I found some bugs in my code. Looking at Go’s packages, I realized that all the tools were already available for me to use. I was just lacking a good example. Walking through the test suite I finally
figured out how to write a simple multipart file upload example with some extra query params.
Hopefully this example will be helpful to some of you.
1 |
|
Example’s source code on GitHub
All the work is done in the newfileUploadRequest
function
and really, the mime/multipart
package hides
all the complexity of creating a multipart request.
The key is to set a new multipart.Writer
:
1 |
|
The writer will do all the work and will write directly to our body (which itself is a buffer of bytes).
We then create a part for the file form entry with the name of the file param and the name of the file (that we extracted using the path/filepath
package).
We need to add the content of the file to the file part, we use the io.Copy()
to
do so. In the first version of this article, I had used io/ioutil
Readall
to
read the content of the file (see code here). However
a few readers rightfully mentioned that I should instead copy content from the file to the part instead of temporarily loading the content of the file in memory. Here is
an even more optimized version using goroutine to stream the data, and here is
the full example using a pipe.
1 |
|
The multipart.Writer
takes care of setting
the boundary and formating the form data for us, nice isn’t it?!
Then for any extra params passed as a map of string keys to string value, we use another function of themultipart.Writer
type:
1 |
|
Once again, the writer takes care of creating the right headers, and to add the passed value.
At this point, we just need to close our writer and use our body to create a new request.
1 |
|
One last thing before triggering our request, we need to set the header that contains the content type including the boundary being used. Once again, the Go lib has us covered:
1 |
|
As a reference, here is the generated body:
1 |
|
Golang might not be as high level as Ruby or Python, but it’s not too far off and it certainly comes with some great std libs. I know I recently caught myself writing a lot of small scripts in Go, something I used to do in Ruby. I think this is mainly due to
the fact that Go is compiled, designed for concurrency, has great std libs and is quite easy to write.
Hopefully this code sample illustrates how easy Go can be and can also serve as a reference point if you are looking for a way to do multipart upload.
Golang Multipart File Upload Example的更多相关文章
- jQuery File Upload 单页面多实例的实现
jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...
- 【转发】Html5 File Upload with Progress
Html5 File Upload with Progress Posted by Shiv Kumar on 25th September, 2010Senior Sof ...
- 《Play for Java》学习笔记(六)文件上传file upload
一. Play中标准方法 使用表单form和multipart/form-data的content-type类型. 1.Form @form(action = routes.Application.u ...
- jQuery File Upload
jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...
- jQuery File Upload blueimp with struts2 简单试用
Official Site的话随便搜索就可以去了 另外新版PHP似乎都有问题 虽然图片都可以上传 但是response报错 我下载的是8.8.7木有问题 但是8.8.7版本结合修改main. ...
- html5 file upload and form data by ajax
html5 file upload and form data by ajax 最近接了一个小活,在短时间内实现一个活动报名页面,其中遇到了文件上传. 我预期的效果是一次ajax post请求,然后在 ...
- Angular2 File Upload
Angular2 File Upload Install Install the components npm install ng2-file-upload --save github: https ...
- [EXP]Adobe ColdFusion 2018 - Arbitrary File Upload
# Exploit Title: Unrestricted # Google Dork: ext:cfm # Date: -- # Exploit Author: Pete Freitag of Fo ...
- 【DVWA】Web漏洞实战之File Upload
File Upload File Upload,即文件上传漏洞,一般的上传漏洞可能是未验证上传后缀 或者是验证上传后缀被bypass 或者是上传的文件验证了上传后缀但是文件名不重命名. LOW 直接上 ...
随机推荐
- iOS9 ReplayKit录制视频
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/50260873 ...
- javascript原始值和对象引用
一句话来说:原始值是不可变的,而对象引用是可变的. js中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着本质的区别.原始值是不可更改的,任何方法都无法更改一 ...
- ruby rails_autolink不能加载的原因
从rails 3.1.0开始,默认在ActionView::Helper::TextHelper中的auto_link方法已经被移除,放到了第三方的gem里:rails_autolink.遂想试一下其 ...
- mac os X中关于dayone缓存的实际文件位置
最近刚安装了mac版的dayone软件,感觉蛮不错的!以前一直用iphone版的,mac版是要米的,68米丫!想了想还是一咬牙:买了! 我用的是iCloud同步,虽然资料放在云中,但本地还是会有缓存的 ...
- Course2-Python函数和模块
一. 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率. 上一课里提到了Python的很多内置函数.在此主要讲自定义函数. 1. 定 ...
- mysql之数据库的增删改查
一.DDL 1.创建数据库 create database 数据库名 *数据库名不能中文, 不能数字正常英文 , 关键字会自动变大写 2.删除数据库 drop database 数据库名 3.使用数据 ...
- c#调用野狗云 rest api
野狗云就不多介绍了,这里主要是记录一下c#调用他们提供的rest api,把数据post到野狗云存储,直接上代码 static void Main(string[] args) { string st ...
- java并发包分析之———ConcurrentSkipListMap
一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下,ConcurrentHashMap 存取速度是ConcurrentSki ...
- 并发编程(五):CAS
在atomic包中,大多数类都是借助unsafe类来实现的,如以下代码 public static AtomicInteger count = new AtomicInteger(0); privat ...
- Linux下高并发socket最大连接数
http://soft.chinabyte.com/os/285/12349285.shtml (转载时原文内容做个修改) 1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是 ...