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 直接上 ...
随机推荐
- PS 图像调整算法——色调分离
色调分离的原理就是将R, G, B每个通道 0-255 的色调区间进行强制划分到给定的区间里去,所以色调会合并,最终的图像看起来颜色就是一块一块的. clc; clear all; close all ...
- Android开发技巧——自定义控件之组合控件
Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...
- SharePoint 使用技巧汇总与讨论
1. 网站内容和结构(/_layouts/sitemanager.aspx) 自己使用SharePoint也有一年了,居然没有发现这个页面,鄙视自己一下,才发现这个页对数据进行操作,会方便很多,比如 ...
- LeetCode(42)-Best Time to Buy and Sell Stock(卖股票)
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- JS(作用域和闭包)
1.对变量提升的理解 1.变量定义(上下文) 2.函数声明 2.说明 this 几种不同的使用场景 常见用法 1.作为构造函数执行 2.作为对象属性执行 3.作为普通函数执行(this === win ...
- OSGi简介
OSGi简介 OSGi是什么 下面来看看“维基百科”给出的解释: OSGi(Open Service Gateway Initiative)有双重含义.一方面它指OSGi Alliance组织:另一方 ...
- 重构:以Java POI 导出EXCEL为例
重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...
- 中文字体名称对照表(unicode码)及20个web安全字体
在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体.但多数情况下,考虑各个因素的影响我们还是在尽量充分利用 ...
- JDK8安装时错误1335的解决
Win7安装JDK8 update65版本时,碰到错误1335,错误信息大概是一个cab文件损坏了,搜索了一下,有网站提供这个错误的修补工具,不过最终我没有下载这个工具,说是系统问题,但工具不是MS官 ...
- PHP代码审计
Preface 这篇文章的内容会不断的充实和丰富,前期会增加一些之前爆出漏洞的复现过程,来丰富自己实际代码审计经验,后期如果能挖掘出新的漏洞,便更好. 代码审计之SQL注入:BlueCMSv1.6 s ...