當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Writer.CreateFormFile方法代碼示例

本文整理匯總了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()
}
開發者ID:dholbach,項目名稱:snappy,代碼行數:35,代碼來源:snap_op.go

示例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
}
開發者ID:shtlee,項目名稱:qa,代碼行數:32,代碼來源:multipart.go

示例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()))
}
開發者ID:first-developer,項目名稱:golang-utils,代碼行數:30,代碼來源:rpc.go

示例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
}
開發者ID:tjyang,項目名稱:resty,代碼行數:9,代碼來源:client.go

示例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
}
開發者ID:tingin,項目名稱:go-httpclient,代碼行數:10,代碼來源:util.go

示例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
}
開發者ID:davecheney,項目名稱:gomaasapi,代碼行數:13,代碼來源:client.go

示例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
}
開發者ID:bssthu,項目名稱:gitlab-ci-multi-runner,代碼行數:12,代碼來源:gitlab.go

示例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
}
開發者ID:rd-2bees,項目名稱:docker,代碼行數:15,代碼來源:upload_test.go

示例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
}
開發者ID:gssdromen,項目名稱:resty,代碼行數:15,代碼來源:client.go

示例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
}
開發者ID:intfrr,項目名稱:sendto,代碼行數:16,代碼來源:net.go

示例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
}
開發者ID:Centny,項目名稱:Cny4go,代碼行數:16,代碼來源:http.go

示例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
}
開發者ID:pjozog,項目名稱:asciinema,代碼行數:17,代碼來源:http.go

示例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()
}
開發者ID:psyvisions,項目名稱:bitwrk,代碼行數:17,代碼來源:buy.go

示例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)
}
開發者ID:lhchavez,項目名稱:quark,代碼行數:45,代碼來源:main.go

示例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
}
開發者ID:lambdaX,項目名稱:rsc.godev,代碼行數:45,代碼來源:rietveld.go


注:本文中的mime/multipart.Writer.CreateFormFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。