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 8143049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:59:27+00:00 2026-06-06T12:59:27+00:00

I am experiencing difficulties with uploading files via ajax without sing html form. My

  • 0

I am experiencing difficulties with uploading files via ajax without sing html form.
My case is like this:

  1. I have a textarea
  2. I would like to zip the content of that textarea and upload it to the server via AJAX (right now I am using JSZip)
  3. For testing purpose, I try to create a dummy zip file to send like this (not getting textarea content):

    var zip = new JSZip();
    zip.file("hello1.txt", "Hello First World\n");<br/>
    zip.file("hello2.txt", "Hello Second World\n");<br/>
    var content = zip.generate();
    
  4. Then I use Jquery ajax method to send, like this:

    $.post("the_url",
        {
            GradeRequest : {
                submitter_id : "foobar",
                evaluationset_id : 9,
                mode : 1,
                source_file : "a.cpp",
                file: content
            }
        }
    );
    

But the file is not received on the server.
I have read question in How can I upload files asynchronously?, but all solutions are using html form. Is there any solution which does not involve any html form ?

Thanks in advance.

In the server side, I’m using Yii PHP Framework:

$model = $this->model;
    if ($model !== null && isset($_POST['GradeRequest'])) {
        $model->setAttributes($_POST['GradeRequest']);

        if ($model->validate()) {
            if (isset($_FILES['GradeRequest']['tmp_name']['file']) && $_FILES['GradeRequest']['tmp_name']['file'] !== "") {
                $model->file = file_get_contents($_FILES['GradeRequest']['tmp_name']['file']);
                $model->source_file = $_FILES['GradeRequest']['name']['file'];
            }

            $this->setReply(true, "Ok");
            $model->client_id = $this->clientId;
            $model->request_id = $this->requestRecord->id;
            $model->save();
        } else {
            $this->setReply(false, $model->getErrors());
        }
    }

As a proof of concept, I have created a form version like this and it works (the file got uploaded to the server):

<form enctype="multipart/form-data" method="post" action="[the_url]>
EvaluationSet id: <input type="text" name="GradeRequest[evaluationset_id]" /><br />
<input type="hidden" name="GradeRequest[mode]" value="0" />
<input type="hidden" name="GradeRequest[submitter_id]" value="Someone" />

Source file : <input type="text" name="GradeRequest[source_file]" /><br />
File : <input type="file" name="GradeRequest[file]" /><br />

<input type="submit" />

  • 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-06T12:59:29+00:00Added an answer on June 6, 2026 at 12:59 pm

    There the FormData object that can help you (it’s part of the so called xmlHttpRequest version 2): this is a compatibility chart, with some linked references and examples.

    With that, you can add key/value pairs to the POST form, including File objects, and send it all via the common send method of xmlHttpRequest.

    File objects can be easily retrieved using a <input type="file"> element, or even using drag & drop from your desktop.

    If you want to upload some file content as a file, you’ll have to create a Blob. This feature is still experimental but supported by Chrome and Firefox (at least… and Safari I guess?):

    var builder = new BlobBuilder();
    builder.append(content);
    var blob = builder.getBlob("application/zip");
    

    Keep in mind that at this moment you’ll have to use MozBlobBuilder in Firefox and WebKitBlobBuilder in Chrome instead.

    In some tutorials I’ve seen that the string is actually converted into a Uint8Array first. Maybe that’s based on a older reference, because the append method of BlobBuilder should accept plain strings too. Never tried it though.

    If your content is a Base64 encoded string, you’ll have to convert it using atob (should be supported by every browser that also supports Blob and FormData).

    Edit: BlobBuilder is now deprecated due to the new draft of the Blob constructor. So everything you’ll have to do to get the Blob is:

    var blob = new Blob([content], "application/zip");
    

    The rest is quite simple:

    var form = new FormData();
    form.append("file", blob);
    

    The problem here is that the file name on the server side is unpredictable and depends on the user agent. I’ve seen some uses of append with a third parameter specifying the file name, but I guess it’s a good idea to send the actual file name to a separate key/value pair in the FormData object.

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

Sidebar

Related Questions

I'm just experiencing JSP form Java, I'm using a Java *.java class, within this
I experiencing a strange behavior of C#. Its some thing like this.. var date
I'm experiencing a difficult ajax error in IE9. I will say up-front that this
I'm experiencing some difficulties configuring our project with Maven and need some help :)
When experiencing networking problems on client machines, I'd like to be able to run
Some reasons i am experiencing this issue on my website. When the website loads
I'm currently experiencing some problems with my server. I have a pool which is
I am experiencing difficulties with an expression that should result in showing or hiding
I'm experiencing difficulties trying to invoke document.ready( function() {}) in my unit tests. Suppose
I'm fairly new to OpenGL, and I seem to be experiencing some difficulties. I've

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.