当前位置: 首页>>代码示例>>Golang>>正文


Golang multipart.Writer类代码示例

本文整理汇总了Golang中mime/multipart.Writer的典型用法代码示例。如果您正苦于以下问题:Golang Writer类的具体用法?Golang Writer怎么用?Golang Writer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Writer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: writeJSONPart

func writeJSONPart(writer *multipart.Writer, contentType string, body Body, compressed bool) (err error) {
	bytes, err := json.Marshal(body)
	if err != nil {
		return err
	}
	if len(bytes) < kMinCompressedJSONSize {
		compressed = false
	}

	partHeaders := textproto.MIMEHeader{}
	partHeaders.Set("Content-Type", contentType)
	if compressed {
		partHeaders.Set("Content-Encoding", "gzip")
	}
	part, err := writer.CreatePart(partHeaders)
	if err != nil {
		return err
	}

	if compressed {
		gz := gzip.NewWriter(part)
		_, err = gz.Write(bytes)
		gz.Close()
	} else {
		_, err = part.Write(bytes)
	}
	return
}
开发者ID:joeljeske,项目名称:sync_gateway,代码行数:28,代码来源:attachment.go

示例2: Write

// Write the attachment to the specified multipart writer.
func (a Attachment) Write(w *multipart.Writer) error {
	headers := make(textproto.MIMEHeader)
	if len(a.Filename) != 0 {
		headers.Add("Content-Type", fmt.Sprintf("%s; name=%s", a.ContentType, a.Filename))
	} else {
		headers.Add("Content-Type", a.ContentType)
	}
	if a.Encoded {
		headers.Add("Content-Transfer-Encoding", "base64")
	} else {
		headers.Add("Content-Transfer-Encoding", "quoted-printable")
	}
	p, err := w.CreatePart(headers)
	if err != nil {
		return err
	}
	if a.Encoded {
		if _, err := p.Write([]byte(a.Content)); err != nil {
			return err
		}
	} else {
		q := quotedprintable.NewWriter(p)
		if _, err := q.Write([]byte(a.Content)); err != nil {
			return err
		}
		return q.Close()
	}
	return nil
}
开发者ID:hectane,项目名称:hectane,代码行数:30,代码来源:attachment.go

示例3: writeMultipartStart

func (m *Message) writeMultipartStart(b *bytes.Buffer, writer *multipart.Writer) error {
	multipart := `
Content-Type: multipart/alternative; charset="UTF-8"; boundary="%s"
`
	_, err := b.WriteString(fmt.Sprintf(multipart, writer.Boundary()))
	return err
}
开发者ID:R510,项目名称:revel,代码行数:7,代码来源:message.go

示例4: 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

示例5: WriteRevisionAsPart

// Adds a new part to the given multipart writer, containing the given revision.
// The revision will be written as a nested multipart body if it has attachments.
func (db *Database) WriteRevisionAsPart(revBody Body, isError bool, compressPart bool, writer *multipart.Writer) error {
	partHeaders := textproto.MIMEHeader{}
	docID, _ := revBody["_id"].(string)
	revID, _ := revBody["_rev"].(string)
	if len(docID) > 0 {
		partHeaders.Set("X-Doc-ID", docID)
		partHeaders.Set("X-Rev-ID", revID)
	}

	if hasInlineAttachments(revBody) {
		// Write as multipart, including attachments:
		// OPT: Find a way to do this w/o having to buffer the MIME body in memory!
		var buffer bytes.Buffer
		docWriter := multipart.NewWriter(&buffer)
		contentType := fmt.Sprintf("multipart/related; boundary=%q",
			docWriter.Boundary())
		partHeaders.Set("Content-Type", contentType)
		db.WriteMultipartDocument(revBody, docWriter, compressPart)
		docWriter.Close()
		content := bytes.TrimRight(buffer.Bytes(), "\r\n")

		part, err := writer.CreatePart(partHeaders)
		if err == nil {
			_, err = part.Write(content)
		}
		return err
	} else {
		// Write as JSON:
		contentType := "application/json"
		if isError {
			contentType += `; error="true"`
		}
		return writeJSONPart(writer, contentType, revBody, compressPart)
	}
}
开发者ID:joeljeske,项目名称:sync_gateway,代码行数:37,代码来源:attachment.go

示例6: WriteMultipartDocument

// Writes a revision to a MIME multipart writer, encoding large attachments as separate parts.
func (db *Database) WriteMultipartDocument(body Body, writer *multipart.Writer) {
	// First extract the attachments that should follow:
	following := []attInfo{}
	for name, value := range BodyAttachments(body) {
		meta := value.(map[string]interface{})
		var info attInfo
		info.contentType, _ = meta["content_type"].(string)
		info.data, _ = decodeAttachment(meta["data"])
		if info.data != nil && len(info.data) > kMaxInlineAttachmentSize {
			info.name = name
			following = append(following, info)
			delete(meta, "data")
			meta["follows"] = true
		}
	}

	// Write the main JSON body:
	jsonOut, _ := json.Marshal(body)
	partHeaders := textproto.MIMEHeader{}
	partHeaders.Set("Content-Type", "application/json")
	part, _ := writer.CreatePart(partHeaders)
	part.Write(jsonOut)

	// Write the following attachments
	for _, info := range following {
		partHeaders := textproto.MIMEHeader{}
		if info.contentType != "" {
			partHeaders.Set("Content-Type", info.contentType)
		}
		partHeaders.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", info.name))
		part, _ := writer.CreatePart(partHeaders)
		part.Write(info.data)
	}
}
开发者ID:jchris,项目名称:sync_gateway,代码行数:35,代码来源:attachment.go

示例7: createFormFile

func createFormFile(writer *multipart.Writer, fieldname, filename string) {
	// Try to open the file.
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	// Create a new form-data header with the provided field name and file name.
	// Determine Content-Type of the file by its extension.
	h := textproto.MIMEHeader{}
	h.Set("Content-Disposition", fmt.Sprintf(
		`form-data; name="%s"; filename="%s"`,
		escapeQuotes(fieldname),
		escapeQuotes(filepath.Base(filename)),
	))
	h.Set("Content-Type", "application/octet-stream")
	if ct := mime.TypeByExtension(filepath.Ext(filename)); ct != "" {
		h.Set("Content-Type", ct)
	}
	part, err := writer.CreatePart(h)
	if err != nil {
		panic(err)
	}

	// Copy the content of the file we have opened not reading the whole
	// file into memory.
	_, err = io.Copy(part, file)
	if err != nil {
		panic(err)
	}
}
开发者ID:jemkzheng,项目名称:revel,代码行数:32,代码来源:testsuite.go

示例8: createBlobletPartWriter

func createBlobletPartWriter(i int, path string, size int64, hash string, writer *multipart.Writer) (io.Writer, error) {
	h := make(textproto.MIMEHeader)
	h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="bloblet-%d"; path="%s"; filename="%s"; hash="%s"`, i, path, bloblet.BlobletFileName, hash))
	h.Set("Content-Type", "application/zip")
	h.Set("Content-Length", fmt.Sprintf("%d", size))
	h.Set("Content-Transfer-Encoding", "binary")
	return writer.CreatePart(h)
}
开发者ID:glyn,项目名称:bloblets,代码行数:8,代码来源:abclient.go

示例9: writeField

func writeField(w multipart.Writer, fieldName string, fieldVal string) error {
	if err := w.WriteField(fieldName, fieldVal); err != nil {
		message := "Can not write field " + fieldName + " into the request. Reason: " + err.Error()
		logging.Logger().Warn(message)
		return errors.New(message)
	}
	return nil
}
开发者ID:hashicorp,项目名称:terraform,代码行数:8,代码来源:opsgenie_alert_client.go

示例10: writeMultipartEnd

func (m *Message) writeMultipartEnd(b *bytes.Buffer, writer *multipart.Writer) error {
	multipart := `
--%s--

`
	_, err := b.WriteString(fmt.Sprintf(multipart, writer.Boundary()))
	return err
}
开发者ID:R510,项目名称:revel,代码行数:8,代码来源:message.go

示例11: createZipPartWriter

func createZipPartWriter(zipStats os.FileInfo, writer *multipart.Writer) (io.Writer, error) {
	h := make(textproto.MIMEHeader)
	h.Set("Content-Disposition", `form-data; name="application"; filename="application.zip"`)
	h.Set("Content-Type", "application/zip")
	h.Set("Content-Length", fmt.Sprintf("%d", zipStats.Size()))
	h.Set("Content-Transfer-Encoding", "binary")
	return writer.CreatePart(h)
}
开发者ID:Jack1996,项目名称:cli,代码行数:8,代码来源:application_bits.go

示例12: 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

示例13: 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

示例14: writeFields

func writeFields(mpw *multipart.Writer, fields map[string]string) error {
	for k, v := range fields {
		err := mpw.WriteField(k, v)
		if err != nil {
			return err
		}
	}
	return nil
}
开发者ID:lambdaX,项目名称:rsc.godev,代码行数:9,代码来源:rietveld.go

示例15: mwSet

func mwSet(mw *multipart.Writer, key, value string) error {
	pw, err := mw.CreateFormField(key)
	if err != nil {
		return err
	}
	if _, err = pw.Write([]byte(value)); err != nil {
		return err
	}
	return nil
}
开发者ID:zhuharev,项目名称:cloudconvert,代码行数:10,代码来源:cloudconvert.go


注:本文中的mime/multipart.Writer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。