我正在开发一个支持文件上传的网络应用程序。我已经熟悉检查服务器端的大小,但我想检查客户端的文件大小。
我知道这是浏览器的限制,出于安全原因我们无法访问文件属性。所以我尝试了 swfupload 和 uploadify 组件。两者都很好并且满足需求。
但限制是都取决于闪存,所以如果我的最终用户没有安装闪存,那么我最终会遇到问题。要求他们安装 Flash 是另一回事,但它是一个门户网站,而且用户群庞大。所以我不喜欢要求他们安装闪存的想法。
今天我遇到了 gmail 中的文件上传功能。并在没有安装 Flash 的情况下在浏览器(IE)中进行了测试。我发现很有趣。如果您上传一个大文件,他们会进行回发并立即返回文件大小太大的消息。
这是怎么可能的,他们如何在不下载整个内容的情况下找到文件的大小?我相信这必须通过读取 HTTP 标头信息来完成。我说得对吗?
这就是我想要实现的确切功能。如果已经安装了闪存,我可以使用 uploadify 来检查大小,否则我可能会实现谷歌从未听说过的技术。
有人可以推荐我怎么做吗?
有人之前遇到过类似的问题,你做了什么?
更新:
在 FF 中安装 Flash 的 Gmail 上传
在 IE 中不安装 Flash
I am working on a web application which supports file uploading. I am already familiar checking the size in server side, but i wanted to check the file size in a client side.
I know its a browser limitation that we cant access the file properties for security reasons. So i have tried both swfupload and uploadify component. Both are good and serve the needs.
But the limitation is both depends on flash, so if my end user not installed flash then i would end up in a problem. Asking them to install flash is another thing, but its a web portal and the user base is huge. So i don't like the idea of asking them to install flash.
Today i came across file upload functionality in gmail. And tested this in browser(IE) without having flash installed. What i found was interesting. If you upload a big file, they made a postback and immediately returning the message the file size is too large.
How this is possible, how they can find the size of the file without downloading the whole content.? I believe this must be done by reading HTTP header info. am i right?
So this is the exact functionality i wanted to implement. If the flash is installed already, i can use uploadify to check the size otherwise may be i ll implement-the-never-heardof-technique-used-by-google.
can someone recommend me how to do this?
anybody faced the similar problems earlier, what have you done??
Update:
Gmail upload with flash installed in FF
without flash in IE
发布评论
评论(10)
您可以查看 File API,它是 HTML 5 的草案。这里是一篇好文章。这就是当然,如果浏览器支持的话,Gmail 会使用。但无法确保这适用于所有浏览器。
You may take a look at the File API which is a draft for HTML 5. Here's a nice article. That's what Gmail uses if the browser supports it of course. But there's no way to ensure this will work across all browsers.
在 IE 中,您可以使用 JS 和 ActiveX:
In IE, you can do it with JS and ActiveX:
事实上,几天前我(或多或少)问过这个问题,一般的答案似乎是:这是不可能的。
供参考:确保文件大小上传文件的数量小于上传前的 maxRequestLength?
I actually asked this very question (more or less) a few days ago and the general answer seems to be: It can't be done.
For reference: Ensure file size of uploaded file is lower than maxRequestLength before the upload occurs?
我还没有测试过这个,但是请求中没有“Content-Length”标头吗?它不仅包括正在上传的文件,而且您可以将其用作基准来确定 POST 是否太大而无法处理。
I haven't tested this, but isn't there a "Content-Length" header on the request? It will include more than just the file being uploaded, but you can use it as a benchmark to determine if the POST is too large to handle.
首先我要说的是,我无法生成工作副本,其中大部分是我通过阅读周围的内容和轻微的经验而理解的。也就是说,也许你可以更多地探索它和/或如果我错了,请纠正我。
主要内容指向带有名为 MAX_FILE_SIZE 的隐藏输入的经典表单的想法...在 PHP/Apache 中,如果上传大于 MAX_FILE_SIZE,服务器将停止(请参阅 此处)。在 PHP 方面,$_FILES 数组将返回一个错误代码(可以看到 此处)。记下UPLOAD_ERR_FORM_SIZE。
为了将这一切放在一起,您可以让 JavaScript 在 IFrame 中上传文件并获取结果。如果服务器发出错误消息(从理论上讲,速度会相当快),JavaScript 可以简单地警告用户。否则,我们可以假设文件现在已成功上传。现在试图理解谷歌的代码几乎是不可能的;据我所知,快速的 HTML 扫描并没有任何帮助...它们确实有隐藏的输入字段,但没有一个以 MAX_FILE_SIZE 作为名称。它们要短得多,而且大多数似乎没有价值。但是,我相信这是可能的。有人想吗?
谁知道呢,也许伟大的 Google Web 服务器内置了立即切断上传的功能。感谢您的聆听!
I'll start off by saying that I haven't been able to generate a working copy and most of this is my understanding from reading around and slight expirience. That said, perhaps you could explore it a bit more and/or set me straight if I'm wrong.
The main thing points to the idea of the classic form with a hidden input named MAX_FILE_SIZE... In PHP/Apache, the server will stop if the upload is larger than the MAX_FILE_SIZE (see last post from here). On the PHP side of things, the $_FILES array will return an error code (which can be seen here). Take note of UPLOAD_ERR_FORM_SIZE.
To put this all together, you could have JavaScript upload the file within an IFrame and get the result. If the server spits out an error message (which, in this theory, would be quite quickly), JavaScript can simply alert the user. Otherwise, we can assume the file is now uploaded successfully. Now trying to make sense of Google's code is near impossible; a quick HTML scan is as far as I go and it didn't help any... They do indeed have hidden input fields but none with MAX_FILE_SIZE as the name. They are much shorter and most don't appear to have values. But, I believe this could be possible. Thoughts anyone?
Who knows, perhaps the great Google Web Server is built in with the power to cut off uploads instantly. Thanks for listening!
我相信文件上传是基于 RFC 1867,尽管 HTML5 取代了这一切。您可以在服务器端完美地重新实现此 RFC,并检查累积传入流的大小。
在最一般的情况下,我认为您不能安全地依赖内容长度,因为它可能不代表文件的大小,而仅代表块的大小。
I believe file upload is based on RFC 1867, although HTML5 supersedes all this. You can perfectly reimplement this RFC, server side, and check the size of the cumulated incoming stream.
In the most general cases, I don't think you can safely rely on the content-length as it may not represent the size of the file, but just the size of a chunk.
经过大量搜索后,我发现我们可以使用 maxAllowedContentLength 限制 ASP.Net 应用程序中的上传大小。请记住,此设置仅适用于 IIS 7 或更高版本。
由于这是在 IIS 级别处理的,因此我们不需要在服务器代码中处理任何内容。如果超出大小限制,则会返回错误代码404.13。这样你就可以很容易地检查客户端的代码来确定问题所在。
如果客户端计算机上未安装闪存,这就是我处理大文件的方法。
希望这可以帮助某人。
有关此设置的详细信息,请阅读此
After lot of searching i found we can use maxAllowedContentLength to limit the upload size in ASP.Net applications. Remember this settings work only on IIS 7 or above.
Since this is handled in IIS level, we dont need to handle anything in server code. If the size limit exceeds, it will return the error code 404.13. So you can easily check the code in client side to determine the problem.
This is what i have done to handle the large files if the flash is not installed in the client machine.
Hope this may help someone.
For more info read about this settings, read this
即使在上传的文件完全传输之前,也可以确定正在上传的文件的大小。即使使用 Internet Explorer 时也是如此。通过使用 HTTP 的内容长度标头!
在Java世界中:
首先,如果您在任何先前的线程质量之前处理第一个 servlet/filter/controller 中到达服务器的请求,您将可以在 content-length 标头中获取总文件大小 (request.getContentLength())。
其次-看看 ajax 实现,证明这是可能的:
http://www.ajaxfilebrowser.com/
It is possible to determine the size of the file that is on uploading even before the uploaded file has been transfered completely. Even when using Internet Explorer. By using the HTTP's content-length header!
In java world:
First of all the if you handle the request that arrive to the server in the first servlet/filter/controller before any previous thread mass with it, you'll be albe to get the total file size in the content-length header (request.getContentLength()).
second- have a look in an ajax implementation that prove that it is possible:
http://www.ajaxfilebrowser.com/
您可以使用以下代码来获取 Gmail 功能:
希望对您有所帮助。
You can have the following Code to get Gmail feature:
Hope this help.
哈哈,ofc,你可以做到,只是有点辛苦,如果你搜索“uploadbar flash”
您可以使用它来传递或比较或执行任何您想要的操作,而不是使用您检索到的数据来创建上传程序
我发现看起来相当不错的是这个,它不需要使用闪光灯
但你可以用它来达到同样的目的
http://www.plupload.com
高度可配置
PS如果你真的喜欢的话,你可以强制它只使用闪光灯
lol ofc you can do it, it's just a bit of hardwork, if you search for "uploadbar flash"
instead of using the data you retrieve to create a uploader you could use it to pass it on or compare or do whatever you want
the one i found that seemed quite good is this one, it doesnt necceraly use flash
but you could use it to achive the same thing
http://www.plupload.com
highly configurable
p.s. you could force it to just use flash if you really like