0

So I'm working on a simple project, it's a basic file upload system. Everything is working and running fine when I test the project locally, but whenever I run this program on my server, the file upload stops working at 100mb. It isn't speed, because at 99mb it uploaded in about 20 seconds. I only get 1 progress update when logging with the following function:

xhr.upload.addEventListener("progress", (e) => {
                const progress = Math.floor(e.loaded/e.total*100)
                progressBar.style.width = progress + "%"
                progressBar.innerHTML = progress+"%"
                console.log(e.loaded/e.total*100)
            }, false)

When with a 99mb file, I get them almost every second. Again, all of this works fine on localhost with no issues, it is just on my server. I am wondering if there is some sort of client or server issue preventing this? I want to allow larger files. Thanks!

client code:

<form id="uploadForm">
    <div class="card" style="width: 50vw;">
        <div class="input-group mb-3">
            <button id="upload" disabled class="btn btn-success">Upload</button>
            <input type="file" name="file" id="file" class="form-control">
        </div>
        <div class="progress">
            <div class="progress-bar" id="progress" role="progressbar" style="width: 0%">0%</div>
        </div>  
    </div>
</form>
<script>
    const fileInput = document.getElementById("file")
    const uploadButton = document.getElementById("upload")
    const progressBar = document.getElementById("progress")

    uploadButton.onclick = () => {
        uploadFile(fileInput.files[0])
    }

    function uploadFile(file) {
        console.log(file)
        const xhr = new XMLHttpRequest()
        if (xhr.upload) {
            const formdata = new FormData()
            uploadButton.disabled = true
            xhr.upload.addEventListener("progress", (e) => {
                const progress = Math.floor(e.loaded/e.total*100)
                progressBar.style.width = progress + "%"
                progressBar.innerHTML = progress+"%"
                console.log(e.loaded/e.total*100)
            }, false)
            xhr.onreadystatechange = (e) => {
                if (xhr.readyState == 4) {
                    const response = JSON.parse(xhr.response)
                    window.location.pathname = response.path
                }
            }
            formdata.append("file", file)
            xhr.open("POST", "/upload", true)
            xhr.setRequestHeader("X-FILENAME", file.name)
            xhr.send(formdata)
        }
    }
    
    fileInput.onchange = event => {
        uploadButton.disabled = fileInput.value ? false : true
    }
</script>

server code: using express and express-fileupload on npm removed a lot of the unneeded stuff

import express from "express"
import fileUpload, { UploadedFile } from "express-fileupload"

const app = express()
app.use(fileUpload({
    limits: {
        files: 1
    }
}))
app.post("/upload", (req, res) => {
    const file = req.files.file as UploadedFile
    const key = crypto.randomBytes(8).toString("hex")
    fs.mkdirSync(path.join("./uploads", key))
    file.mv(path.join("./uploads", key, file.name))
    res.json({
        file: file.name,
        path: `/uploads/${key}/${file.name}`
    })
})
TKDKid1000
  • 33
  • 1
  • 5
  • You need to configure your server to accept file uploads grater than 100mb. With loaclhost its common that you dont need to explicitly specify but on dev/prod server this is necessay. Here is a link to the stackoverflow question that answers your actual problem. https://stackoverflow.com/questions/13374238/how-to-limit-upload-file-size-in-express-js – Mujeeb Qureshi Mar 22 '22 at 14:39
  • This didn't really work, I think my issue is more client side than server side, I just included server code in case it was necessary. If you go to https://files.tkdkid1000.net and upload something above 99mb you will see only one progress message in the javascript console. 99mb and lower and everything works just fine, with consistent(ish) upload progress messages. – TKDKid1000 Mar 23 '22 at 01:15
  • I just managed to send a 270mb file on your server (the link you provided in your comment). – Mujeeb Qureshi Mar 23 '22 at 01:44
  • and on 100% completion I am getting an error code 413 - Request Entity Too Large from cloudfare, I suppose thats the server where you have hosted your application. – Mujeeb Qureshi Mar 23 '22 at 01:52
  • I presume this stackoveflow question's answer addresses your problem: https://stackoverflow.com/questions/19917401/error-request-entity-too-large Then again, its **not** frontend related! – Mujeeb Qureshi Mar 23 '22 at 01:56

0 Answers0