Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8481367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:32:36+00:00 2026-06-10T19:32:36+00:00

I need to upload a part of a file (only the first MB). I’ve

  • 0

I need to upload a part of a file (only the first MB). I’ve created a PHP script which uploads the whole file. The data (formData Object) is passed by a ajax call.

My idea would be now to split the file with javascript (jquery). Is there any solution for my request?

Current code:

function start(a){
    //var fSize = $('#fileUpload')[0].files[0].size / 1024;
    var formData = new FormData();    
    formData.append( 'fileUpload', $('#fileUpload')[0].files[0] );
    //AJAX
    $.ajax({
        url: 'script.php',
        type: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        success: function(msg){
            alert("Win: " + msg);
        },
        error: function(bla, msg){
            alert("Fail: " + msg);
        }
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:32:37+00:00Added an answer on June 10, 2026 at 7:32 pm

    Since you’re using FormData, which is a fairly new technology, I’ll show you something with new technologies as well.

    First, read the file with a FileReader object:

    var fr = new FileReader(), buf, file = $('#fileUpload')[0].files[0];
    fr.onload = function(e) {
        buf = new Uint8Array(e.target.result);
    };
    fr.readAsArrayBuffer(file);
    

    Then you can create a Blob for each splitted part (1e6 bytes long each):

    for (var i = 0, blobs = []; i < buf.length; i += 1e6)
        blobs.push(new Blob([buf.subarray(i, i + 1e6)]));
    

    Finally, you can add all your Blobs to your FormData object:

    var formData = new FormData();
    for (var i = 0; i < blobs.length; i++)
        formData.append("slice" + i, blobs[i], file.name + ".part" + i);
    

    You should be ok. I haven’t tested it, though.

    I don’t know anything about the performance either. You can use fr.readAsBinaryString too, thus making e.target.result a string. This way, you can create the Blobs using a simple substring/slice/substr/whatever, but I fear there could be some problems with Unicode characters and whatnot. Plus, maybe it’s slower.

    Putting everything in a more coherent snippet:

    $('#fileUpload').change(function() {
        // If no file is selected, there's nothing to do
        if (!this.files.length) return;
    
        var fr = new FileReader(), file = this.files[0];
        fr.onload = function(e) {
            splitAndSendFile(new Uint8Array(e.target.result), file);
        };
        fr.readAsArrayBuffer(file);
    };
    
    function splitAndSendFile(dataArray, file) {
        var i = 0, formData, blob;
        for (; i < dataArray.length; i += 1e6) {
            blob = new Blob([dataArray.subarray(i, i + 1e6)]);
            formData = new FormData();
            formData.append("fileUpload", blob, file.name + ".part" + (i / 1e6));
            $.ajax({
                url: 'script.php',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(msg){
                    alert("Win: " + msg);
                },
                error: function(bla, msg){
                    alert("Fail: " + msg);
                }
            });
        }
    }
    

    Note: FormData.append takes a third optional parameter, which should be the name of the file in case of File or Blob values. If not specified, Blobs may get unpredictable random file names.

    Probably that parameter isn’t standard, and it’s not mentioned in the MDN artice, but I used it in the snippet above nonetheless. Anyway, if you know what you’re doing, you can have several options to specify the file name. For example, with formData.append("filename", file.name) or sending a custom header in the request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an upload PHP script and before it uploads the file, it renames
I need to upload my app file through Application Loader 2.7. But first I
the procedure: i have a script which uploads images (via hmtl-form and php) for
I am working on a file upload script. I know that every part of
I need to have as part of a desktop application a file server which
I need to upload an image as part of a create action in an
I need to upload my customized excel file in magento admin panel. This excel
I need to upload a xml file from server side, where the contents of
I need to upload a potentially huge plain-text file to a very simple wsgi-app
I need to upload a file from the client to an asp.net page. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.