本文整理汇总了Golang中mime/multipart.Writer.CreateFormFile方法的典型用法代码示例。如果您正苦于以下问题:Golang Writer.CreateFormFile方法的具体用法?Golang Writer.CreateFormFile怎么用?Golang Writer.CreateFormFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mime/multipart.Writer
的用法示例。
在下文中一共展示了Writer.CreateFormFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sendSnapFile
func sendSnapFile(snapPath string, snapFile *os.File, pw *io.PipeWriter, mw *multipart.Writer, action *actionData) {
defer snapFile.Close()
if action.SnapOptions == nil {
action.SnapOptions = &SnapOptions{}
}
errs := []error{
mw.WriteField("action", action.Action),
mw.WriteField("name", action.Name),
mw.WriteField("snap-path", action.SnapPath),
mw.WriteField("channel", action.Channel),
mw.WriteField("devmode", strconv.FormatBool(action.DevMode)),
}
for _, err := range errs {
if err != nil {
pw.CloseWithError(err)
return
}
}
fw, err := mw.CreateFormFile("snap", filepath.Base(snapPath))
if err != nil {
pw.CloseWithError(err)
return
}
_, err = io.Copy(fw, snapFile)
if err != nil {
pw.CloseWithError(err)
return
}
mw.Close()
pw.Close()
}
示例2: Write
func Write(w *multipart.Writer, params map[string][]string) error {
for key, param1 := range params {
param := param1[0]
if len(param) > 0 && param[0] == '@' {
file := param[1:]
fw, err := w.CreateFormFile(key, file)
if err != nil {
log.Println("CreateFormFile failed:", err)
return err
}
fd, err := os.Open(file)
if err != nil {
log.Println("Open file failed:", err)
return err
} else {
_, err = io.Copy(fw, fd)
fd.Close()
if err != nil {
log.Println("Copy file failed:", err)
return err
}
}
} else {
err := w.WriteField(key, param)
if err != nil {
return err
}
}
}
return nil
}
示例3: PostWithMultiPartData
func (r Client) PostWithMultiPartData(req_url string, headers map[string][]string, params map[string][]string, body io.Reader, filename string) (resp *http.Response, err error) {
var buffer *bytes.Buffer = new(bytes.Buffer)
var writer *multipart.Writer = multipart.NewWriter(buffer)
for k, v := range params {
for _, field := range v {
err1 := writer.WriteField(k, field)
if err1 != nil {
err = err1
return
}
}
}
w, err := writer.CreateFormFile("file", filename)
if err != nil {
return
}
_, err = io.Copy(w, body)
if err != nil {
return
}
err = writer.Close()
if err != nil {
return
}
if headers == nil {
headers = make(map[string][]string)
}
headers["Content-Type"] = []string{writer.FormDataContentType()}
return r.Post(req_url, headers, buffer, int64(buffer.Len()))
}
示例4: addFileReader
func addFileReader(w *multipart.Writer, f *File) error {
part, err := w.CreateFormFile(f.ParamName, f.Name)
if err != nil {
return err
}
_, err = io.Copy(part, f.Reader)
return err
}
示例5: addFormFileFromData
// Add a file to a multipart writer.
func addFormFileFromData(writer *multipart.Writer, name, path string, data []byte) error {
part, err := writer.CreateFormFile(name, filepath.Base(path))
if err != nil {
return err
}
_, err = part.Write(data)
return err
}
示例6: writeMultiPartFiles
// writeMultiPartFiles writes the given files as parts of a multipart message
// using the given writer.
func writeMultiPartFiles(writer *multipart.Writer, files map[string][]byte) error {
for fileName, fileContent := range files {
fw, err := writer.CreateFormFile(fileName, fileName)
if err != nil {
return err
}
io.Copy(fw, bytes.NewBuffer(fileContent))
}
return nil
}
示例7: createArtifactsForm
func (n *GitLabClient) createArtifactsForm(mpw *multipart.Writer, reader io.Reader, baseName string) error {
wr, err := mpw.CreateFormFile("file", baseName)
if err != nil {
return err
}
_, err = io.Copy(wr, reader)
if err != nil {
return err
}
return nil
}
示例8: writeMPBody
func writeMPBody(fname string, mw *multipart.Writer) error {
fw, _ := mw.CreateFormFile("files[]", filepath.Base(fname))
f, err := os.Open(fname)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(fw, f)
if err != nil {
return err
}
return nil
}
示例9: addFile
func addFile(w *multipart.Writer, fieldName, path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
part, err := w.CreateFormFile(fieldName, filepath.Base(path))
if err != nil {
return err
}
_, err = io.Copy(part, file)
return err
}
示例10: addFile
// addFile adds a file to this multipart form
func addFile(w *multipart.Writer, filePath string) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
fw, err := w.CreateFormFile("file", filePath)
if err != nil {
return err
}
_, err = io.Copy(fw, f)
if err != nil {
return err
}
return nil
}
示例11: CreateFileForm
func CreateFileForm(bodyWriter *multipart.Writer, fkey, fp string) error {
fileWriter, err := bodyWriter.CreateFormFile(fkey, fp)
if err != nil {
return err
}
fh, err := os.Open(fp)
if err != nil {
return err
}
defer fh.Close()
_, err = io.Copy(fileWriter, fh)
if err != nil {
return err
}
return nil
}
示例12: addFormFile
func addFormFile(writer *multipart.Writer, name string, reader io.Reader) error {
items := strings.Split(name, ":")
fieldname := items[0]
filename := items[1]
part, err := writer.CreateFormFile(fieldname, filename)
if err != nil {
return err
}
_, err = io.Copy(part, reader)
if err != nil {
return err
}
return nil
}
示例13: encodeChunkedFirstTransmission
func (a *BuyActivity) encodeChunkedFirstTransmission(log bitwrk.Logger, mwriter *multipart.Writer) (err error) {
part, err := mwriter.CreateFormFile("a32chunks", "a32chunks.bin")
if err != nil {
return
}
log.Printf("Sending work chunk hashes to seller [%v].", *a.tx.WorkerURL)
err = cafs.WriteChunkHashes(a.workFile, part)
if err != nil {
return
}
log.Printf("Sending buyer's secret to seller.")
err = mwriter.WriteField("buyersecret", a.buyerSecret.String())
if err != nil {
return
}
return mwriter.Close()
}
示例14: gradeRun
func gradeRun(
ctx *common.Context,
client *http.Client,
run *common.Run,
multipartWriter *multipart.Writer,
) (*runner.RunResult, error) {
ctx.EventCollector.Add(ctx.EventFactory.NewEvent(
"grade",
common.EventBegin,
common.Arg{"id", run.AttemptID},
common.Arg{"input", run.InputHash},
common.Arg{"language", run.Language},
))
defer func() {
ctx.EventCollector.Add(ctx.EventFactory.NewEvent(
"grade",
common.EventEnd,
))
}()
// Make sure no other I/O is being made while we grade this run.
ioLockEvent := ctx.EventFactory.NewCompleteEvent("I/O lock")
ioLock.Lock()
defer ioLock.Unlock()
ctx.EventCollector.Add(ioLockEvent)
inputEvent := ctx.EventFactory.NewCompleteEvent("input")
input, err := inputManager.Add(
run.InputHash,
runner.NewRunnerInputFactory(client, &ctx.Config),
)
if err != nil {
return nil, err
}
defer input.Release(input)
ctx.EventCollector.Add(inputEvent)
// Send the header as soon as possible to avoid a timeout.
filesWriter, err := multipartWriter.CreateFormFile("file", "files.zip")
if err != nil {
return nil, err
}
return runner.Grade(ctx, filesWriter, run, input, &minijail)
}
示例15: write
func (h *baseUploadHandler) write(mpw *multipart.Writer) error {
logf("Uploading base of %s...", h.filepath)
var diff *FileDiff
for _, d := range h.op.patch {
if d.Path == h.filepath {
diff = d
break
}
}
if diff == nil {
return fmt.Errorf("file %s is not part of patch", h.filepath)
}
fields := map[string]string{
"filename": h.filepath,
"status": string(diff.Op),
"is_binary": "false",
"is_current": "false",
"checksum": h.op.baseMD5[h.filepath],
}
//"file_too_large": "1",
if err := writeFields(mpw, fields); err != nil {
return err
}
data, err := mpw.CreateFormFile("data", h.filepath)
if err != nil {
return err
}
if diff.Op == Added {
return nil
}
base, err := h.op.delta.Base(h.filepath)
if err != nil {
return err
}
_, err = io.Copy(data, base)
base.Close()
return err
}