Get file size from multipart form upload Content-Length header

て烟熏妆下的殇ゞ 提交于 2019-11-27 22:11:20

问题


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

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!

File sizes:

-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 Header

{ 
  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

File size is 5379906

Content-Length is reported as 5380216

why?


回答1:


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

Found this as a reference: 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.

EDIT

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!




回答2:


The data you received is always bigger than you file. Just scan the "boundary" value for the starting and ending of the file.

The reference: How does HTTP file upload work?



来源:https://stackoverflow.com/questions/11300641/get-file-size-from-multipart-form-upload-content-length-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!