How to know the file size when uploading via multipart form-data?

China☆狼群 提交于 2019-11-28 08:06:13

问题


I am exercising a simple application with HTML client that uploads a file to the server and must display a progress bar :)

The server is servlet-based, some servlet receives the multipart content, parses it etc. Obviously, at every moment I know the number of bytes received so far. However in order to supply an info for the progress bar on the client side a servlet also needs the total number of bytes for the file, which is being uploaded. Any idea where to get the total number of bytes from please?

Thanks!


回答1:


Size of content are placed in Content-Length header (see rfc2616). In Servlet you can get this header's value by request.getContentLength() or reqest.getHeaders("Content-Length")




回答2:


I think you have to implement the progress bar in javascript on the client side.

Anyway, this link gives an explanation of how to do it using JQuery.


The server can obtain the size of the multipart being uploaded from the HTTP request header; see @ilya's answer. However, getting that information and the count(s) of bytes read so far into a stream of HTTP responses that display or update a progress bar would be rather complicated ...



来源:https://stackoverflow.com/questions/5920689/how-to-know-the-file-size-when-uploading-via-multipart-form-data

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