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


Golang Writer.Boundary方法代码示例

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


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

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

示例2: SendMediaRequest

func SendMediaRequest(client *twittergo.Client, reqUrl string, params map[string]string, media []byte) (mediaResp twittergo.MediaResponse, err error) {
	var (
		req         *http.Request
		resp        *twittergo.APIResponse
		body        io.ReadWriter = bytes.NewBufferString("")
		mp          *multipart.Writer
		writer      io.Writer
		contentType string
	)
	mp = multipart.NewWriter(body)
	for key, value := range params {
		mp.WriteField(key, value)
	}
	if media != nil {
		if writer, err = mp.CreateFormField("media"); err != nil {
			return
		}
		writer.Write(media)
	}
	contentType = fmt.Sprintf("multipart/form-data;boundary=%v", mp.Boundary())
	mp.Close()
	if req, err = http.NewRequest("POST", reqUrl, body); err != nil {
		return
	}
	req.Header.Set("Content-Type", contentType)
	if resp, err = client.SendRequest(req); err != nil {
		return
	}
	err = resp.Parse(&mediaResp)
	return
}
开发者ID:carriercomm,项目名称:twittergo-examples,代码行数:31,代码来源:main.go

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

示例4: writeContentTypeWithBoundary

func (m *Message) writeContentTypeWithBoundary(b *bytes.Buffer, writer *multipart.Writer, contentType string) error {
	contentTypeFormat := `

--%s
Content-Type: %s; charset=UTF-8;
Content-Transfer-Encoding: 8bit

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

示例5: GetBody

func GetBody() (body io.ReadWriter, header string, err error) {
	var (
		mp     *multipart.Writer
		media  []byte
		writer io.Writer
	)
	body = bytes.NewBufferString("")
	mp = multipart.NewWriter(body)
	media, err = ioutil.ReadFile("tweet_media/media.png")
	if err != nil {
		return
	}
	mp.WriteField("status", fmt.Sprintf("Hello %v!", time.Now()))
	writer, err = mp.CreateFormFile("media[]", "media.png")
	if err != nil {
		return
	}
	writer.Write(media)
	header = fmt.Sprintf("multipart/form-data;boundary=%v", mp.Boundary())
	mp.Close()
	return
}
开发者ID:carriercomm,项目名称:twittergo-examples,代码行数:22,代码来源:main.go

示例6: Bytes

// Bytes gets the encoded MIME message.
func (m *Message) Bytes() ([]byte, error) {
	var buffer = &bytes.Buffer{}

	header := textproto.MIMEHeader{}

	var err error

	// Require To, Cc, or Bcc
	// We'll parse the slices into a list of addresses
	// and then make sure that list isn't empty.
	toAddrs := getAddressListString(m.To)
	ccAddrs := getAddressListString(m.Cc)
	bccAddrs := getAddressListString(m.Bcc)

	var hasTo = toAddrs != ""
	var hasCc = ccAddrs != ""
	var hasBcc = bccAddrs != ""

	if !hasTo && !hasCc && !hasBcc {
		return nil, ErrMissingRecipient
	}

	if hasTo {
		header.Add("To", toAddrs)
	}
	if hasCc {
		header.Add("Cc", ccAddrs)
	}
	// BCC header is excluded on purpose.
	// BCC recipients aren't included in the message
	// headers and are only used at the SMTP level.

	var emptyAddress mail.Address
	// Require From address
	if m.From == emptyAddress {
		return nil, ErrMissingFromAddress
	}
	header.Add("From", m.From.String())

	// Optional ReplyTo
	if m.ReplyTo != emptyAddress {
		header.Add("Reply-To", m.ReplyTo.String())
	}

	// Optional Subject
	if m.Subject != "" {
		quotedSubject := qEncodeAndWrap(m.Subject, 9 /* len("Subject: ") */)
		if quotedSubject[0] == '"' {
			// qEncode used simple quoting, which adds quote
			// characters to email subjects.
			quotedSubject = quotedSubject[1 : len(quotedSubject)-1]
		}
		header.Add("Subject", quotedSubject)
	}

	// Date
	if _, ok := m.Headers["Date"]; !ok {
		header.Add("Date", time.Now().UTC().Format(time.RFC822))
	}

	for k, v := range m.Headers {
		header[k] = v
	}

	header.Add("MIME-Version", "1.0")

	var mixedw *multipart.Writer
	var hasAttachments = m.Attachments != nil && len(m.Attachments) > 0
	if hasAttachments {
		// Top level multipart writer for our `multipart/mixed` body.
		// only needed if we have attachments
		mixedw = multipart.NewWriter(buffer)
		header.Add("Content-Type", fmt.Sprintf("multipart/mixed;%s boundary=%s", crlf, mixedw.Boundary()))
		err = writeHeader(buffer, header)
		if err != nil {
			return nil, err
		}
		header = textproto.MIMEHeader{}
		// Write the start of our `multipart/mixed` body.
		_, err = fmt.Fprintf(buffer, "--%s%s", mixedw.Boundary(), crlf)
		if err != nil {
			return nil, err
		}
	}

	var altw *multipart.Writer
	if m.Body != "" && m.HTMLBody != "" {
		// Nested multipart writer for our `multipart/alternative` body.
		altw = multipart.NewWriter(buffer)

		header.Add("Content-Type", fmt.Sprintf("multipart/alternative;%s boundary=%s", crlf, altw.Boundary()))
		err := writeHeader(buffer, header)
		if err != nil {
			return nil, err
		}
	}

	// Only include an empty plain text body if the html body is also empty.
	if m.Body != "" || m.Body == "" && m.HTMLBody == "" {
//.........这里部分代码省略.........
开发者ID:nkovacs,项目名称:gophermail,代码行数:101,代码来源:main.go


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