求高手指教,网页上传文件是如何实现的?

2024-11-30 11:05:54
推荐回答(1个)
回答1:

 其实有时候答案应该自己去寻找,你既然知道做法,直接抓个包不就知道流程了吗,

  1. 选择文件之后,浏览器只是得到了文件的路径,就是你看到的文本框里的值.

  2. 点击上传后,会给服务器发送类似以下格式的封包,这是原始封包,服务器收到之后,会进行解析才会到达动态脚本,比如PHP,

以下是我上传了一张桌面上的placeholder.gif图片文件,所发送给服务器的封包.里面包括了文件的一些信息和内容.你看下.他们用HTTP头信息里的分隔符分开.

POST /up.php HTTP/1.1

Accept: */*

Referer: http://www.xxx.com/

Accept-Language: zh-cn

Content-Type: multipart/form-data; boundary=---------------------------7df2ef0c00e4

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

Host: www.xxx.com

Content-Length: 10810

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: lang=zh-cn; _ga=GA1.2.447688366.1439125779; _gat=1; Hm_lvt_08e32ec74a8a7982c7171bde829b77af=1439125779; Hm_lpvt_08e32ec74a8a7982c7171bde829b77af=1439125779

 

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="UPLOAD_IDENTIFIER"

 

KEYf47f469d71334b22479450fa3761a026

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="langkey"

 

1

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="setcookie"

 

1

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="tempvar"

 

 

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="upfile"; filename="C:\Documents and Settings\Administrator\......\placeholder.gif"

Content-Type: image/gif

 

(文件二进制数据)

-----------------------------7df2ef0c00e4

Content-Disposition: form-data; name="fpath"

 

C:\Documents and Settings\Administrator\......\placeholder.gif

-----------------------------7df2ef0c00e4--