从多部分表单上传Content-Length标头获取文件大小 [英] Get file size from multipart form upload Content-Length header

查看:856
本文介绍了从多部分表单上传Content-Length标头获取文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我上传了一个文件,然后下载了......但是上传标题中的Content-Length与文件大小不匹配。我猜这个计算和转移涉及到其他一些数据,但具体到底是什么?如何从Content-Length获得准确的文件大小?它甚至可能吗?

So, I'm uploading a file, and then downloading it... but the Content-Length in the upload headers do not match the file size. I'm guessing there is some other data involved with this calculation and transfer, but what exactly? How do I get an accurate file size from the Content-Length? Is it even possible?

我知道有边界,也许还有其他一些东西,但检查一下:

I know there are the boundaries, and maybe some other things, but check it:

5380216 - 5379906 = 310

"----WebKitFormBoundaryeoFyqD4zr6smwYDG".size
→ 38

所以边界大小为38 ... 310有一些5,10,31的LCD ...那些在任何38都没有用方式...我输了!

So the boundary size is 38... 310 has some LCDs of 5, 10, 31... those don't work well with 38 in any way... I'm lost!

文件大小:

-rw-r--r--@  1 williamcotton  staff    5379906 Jul  2 12:02 testfile-downloaded.zip
-rw-r--r--@  1 williamcotton  staff    5379906 Jun  8 14:23 testfile-uploaded.zip

HTTP标头

{ 
  host: 'localhost:8887',
  connection: 'keep-alive',
  'content-length': '5380217',
  'cache-control': 'max-age=0',
  origin: 'http://localhost:8887',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1193.0 Safari/537.1',
  'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryeoFyqD4zr6smwYDG',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  referer: 'http://localhost:8887/',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8',
  'accept-charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3' 
}

tl; dr

文件大小为5379906

File size is 5379906

Content-Length报告为5380216

Content-Length is reported as 5380216

为什么?

推荐答案

我认为multipart / form-data请求的每个部分都计入主Content-Length头的总大小。

I think that each section of the multipart/form-data request counts toward the overall size of the main Content-Length header.

发现这个作为参考:
http://chxo.com/be2/20050724_93bf.html

所以,也许在你的标题之后,你可能有:

So, perhaps after your headers, you might have:

----WebKitFormBoundaryeoFyqD4zr6smwYDG

Content-Disposition: form-data; name="file"; filename="testfile-downloaded.zip"; size=<file size could be here>
Content-Length: <file size could also be here>
<some other content headers>

<file data>

----WebKitFormBoundaryeoFyqD4zr6smwYDG

所有这些都将计入内容 - 长度。

All of which would count toward the Content-Length size.

编辑

刚才意识到我没有回答你的问题。不确定如何从请求的文件部分提取文件大小。对不起!

Just realized I didn't answer your question. Not sure how to pull out the file size from the file section of the request. Sorry!

这篇关于从多部分表单上传Content-Length标头获取文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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