如何解析HTTP POST文件上传流? [英] how to parse HTTP POST(file upload) stream?

查看:1303
本文介绍了如何解析HTTP POST文件上传流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ActionScript引擎上传文件,引擎会选择该文件并发送文件通过网络直通HTTP POST命令,文件说的POST信息是这样的:

I am using actionscript engine to upload a file, the engine will select the file and send the file over network thru HTTP POST command, the document says the POST message is like:

POST /handler.cfm HTTP/1.1
  Accept: text/*
  Content-Type: multipart/form-data; 
  boundary=----------Ij5ae0ae0KM7GI3KM7ei4cH2ei4gL6 
  User-Agent: Shockwave Flash 
  Host: www.example.com 
  Content-Length: 421 
  Connection: Keep-Alive 
  Cache-Control: no-cache

  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
  Content-Disposition: form-data; name="Filename"

  MyFile.jpg
  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
  Content-Disposition: form-data; name="photo"; filename="MyFile.jpg"
  Content-Type: application/octet-stream

  FileDataHere
  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
  Content-Disposition: form-data; name="Upload"

  Submit Query

  ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7--

在服务器端我有一个80端口的C ++程序监听和解析POST信息。我只想要的文件名和文件数据。如何去$ C C使用C ++文件数据$,是它的Base64 EN codeD,是有一个图书馆能为我做?我想脱$ C C二进制$,并将其写入文件,非常感谢!

In server side I have a C++ program listens on port 80 and parse the POST message. I only want the file name and file data. How to decode the file data using c++, is it base64 encoded and is there a library can do it for me? I want to decode the binary, and write it to the file, thanks!

推荐答案

没有,没有编码。多部分消息的每个子部分的主体是作为逐字字节。因此,你要小心选择边界字符串,它不会出现在该文件数据的任何地方。

No, there is no encoding. The body of each subpart of the multipart message is included as verbatim bytes. Consequently you have to be careful to choose a boundary string that doesn't occur anywhere in the file data.

要解析的multipart / form-data的表单提交你将一个MIME解析器足以解析头,挑选出你想要的参数,如边界名称,并通过边界字符串分割接收到的消息。这不完全是微不足道的,所以你可能要考虑现有的库

To parse a multipart/form-data form submission you will enough of a MIME parser to parse the headers, pick out parameters you want like boundary and name, and split the received message by the boundary string. This is not completely trivial, so you may want to consider existing libraries.

(不幸的是,浏览器实际上做的HTTP不同,有些从 RFC 1341 ,特别是字段名和文件名参数往往包括非ASCII字符和转义引号,但如果要生成的POST自己希望你可以避开争夺这些领域。)

(Unfortunately, what browsers actually do in HTTP differs a bit from the standard MIME rules outlined in RFC 1341. In particular, field name and filename parameters tend to include non-ASCII characters and unescaped quotes. But if you are generating the POST yourself hopefully you can steer clear of these areas of contention.)

这篇关于如何解析HTTP POST文件上传流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆