Javascript 文件上传的大小限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28605393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:04:55  来源:igfitidea点击:

Size limit for file upload

javascripthtml

提问by Mike Mameko

I investigate the problem of file upload using html5, and I have theoretical question: has html5 any limits for file size for uploading? For example, can I upload files with size ~500Gb?

我调查了使用 html5 上传文件的问题,我有一个理论问题:html5 对上传的文件大小有任何限制吗?例如,我可以上传大小约 500Gb 的文件吗?

P.S.: I use FileReader api for reading file.

PS:我使用 FileReader api 读取文件。

Ok. This problem is resolved.

好的。此问题已解决。

But as it explains in FileReader API:

但正如它在 FileReader API 中解释的那样:

This interface provides methods to read File objects or Blob objects into memory...

该接口提供了将 File 对象或 Blob 对象读入内存的方法...

As I understood correctly, I can't read file with size which is more than available RAM?

正如我正确理解的那样,我无法读取大小超过可用 RAM 的文件?

回答by atmd

Nope, there is no size upload limit.

不,上传没有大小限制。

here is the specand here is a related question on how to check the file size, so that you can add limits if you like.

这是规范,这是关于如何检查文件大小的相关问题,以便您可以根据需要添加限制。

It's worth pointing out that if you are looking to store the file on the server, you might hit file upload limits/restrictions there. But you should be able to configure them. i.e. php/wordpress default upload limit

值得指出的是,如果您希望将文件存储在服务器上,您可能会在那里遇到文件上传限制/限制。但是您应该能够配置它们。即 php/wordpress 默认上传限制

回答by Hari Krishnan

Hope this useful..

希望这有用..

Form,script for validating:

表单,验证脚本:

<form action="check.php" method="post" enctype="multipart/form-data">
<label>Upload An Image</label>
<input type="file" id="file_upload" name="file_upload" />
<input type="submit" onClick="return validate()" name="upload"/>
</form>

<script>
function validate(){
var size=2097152;
var file_size=document.getElementById('file_upload').files[0].size;
if(file_size>=size){
    alert('File too large');
    return false;
}
var type='image/jpeg';
var file_type=document.getElementById('file_upload').files[0].type;
if(file_type!=type){
    alert('Format not supported,Only .jpeg images are accepted');
    return false;
}
}

php for uploading:

用于上传的php:

 <?php 
        if(isset($_POST['upload'])){

                $target_dir = "uploads/";
                $target_file = $target_dir . basename($_FILES["file_upload"]["name"]);
                if(move_uploaded_file($_FILES["file_upload"]["tmp_name"], $target_file)){ 
                echo "The file ". basename( $_FILES["file_upload"]["name"]). " has been uploaded.";
                }
                else{
                    echo "sorry";
                    }

        }
    ?>