上传前检查文件大小

发布于 2024-12-01 02:59:20 字数 273 浏览 11 评论 0原文

我有一个允许用户上传视频文件的网站,但我想将文件大小限制为 2MB,上传文件后这非常简单,但有人知道我是否可以在上传之前检查文件大小这样我就不会不必要地使服务器超载?

我可能正在寻找 javascript/ajax 解决方案,但我不知道如何实现这一点。

任何帮助将非常受欢迎。谢谢你们!

更新:

我需要检查客户端上的文件大小,而不是服务器端上的文件大小,这可能吗?

I have a website that allows users to upload video files but I want to limit the filesize to 2MB , this is pretty straightforward after uploading the file but does anyone knows if I can check the file-size before uploading it so I don't overload the server unnecessarily ?

I might be looking for a javascript/ajax solution but I don't have a clue on how to achieve this.

Any help will be very welcome. Thanks guys!

UPDATE:

I need to check the file size on client-side and NOT on server-side, is this possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2024-12-08 02:59:20

您可以使用 HTML5 中的文件读取器读取 JavaScript 中的本地文件:
http://www.html5rocks.com/en/tutorials/file/dndfiles/

function handleFileSelect(evt) {
  var files = evt.target.files; // FileList object

  // files is a FileList of File objects. List some properties.
  var output = [];
  for (var i = 0, f; f = files[i]; i++) {
    console.log(f.size);        /*<--Here is your size of the file! :D*/
  }
}

document.getElementById('files').addEventListener('change', handleFileSelect, false);

You can read local files in JavaScript using file reader in HTML5:
http://www.html5rocks.com/en/tutorials/file/dndfiles/

function handleFileSelect(evt) {
  var files = evt.target.files; // FileList object

  // files is a FileList of File objects. List some properties.
  var output = [];
  for (var i = 0, f; f = files[i]; i++) {
    console.log(f.size);        /*<--Here is your size of the file! :D*/
  }
}

document.getElementById('files').addEventListener('change', handleFileSelect, false);
只涨不跌 2024-12-08 02:59:20

如果您使用 php,您可以设置 php 选项 - 'upload_max_filesize'。请参阅 http://www.php.net/ Manual/en/ini.core.php#ini.upload-max-filesize 可能有帮助。

If you use php you can set php option - 'upload_max_filesize'. See http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize May be it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文