Jump to content

post form data (textarea) as file


Go to solution Solved by requinix,

Recommended Posts

While I can post large files, I cannot post form "data" larger than 1MB.

 

On the submit event I collect the form's textarea data.  What I'd like to do is somehow

place that data into a form file and post that instead.  Can it be done?  When I say "form file"

I'm talking about the client posted data, saved server side, in the $_FILES[] array.

 

I'm partial to jQuery so feel free to use that package in any example. 

 

No, this isn't a school assignment.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/307112-post-form-data-textarea-as-file/
Share on other sites

https://forums.phpfreaks.com/topic/307038-split-form-textarea-data-for-multiple-posts/

 

I can upload HUGE form 'file' types.  The post fails when 'other' types reach exactly 1MB.

I serialize the form data and watch the length.

 

I really don't want to go the split data route.

Edited by fatkatie

Originally I failed to define the enctype, but per the other post's suggestion I included it.  Still failed.  Here's the code.

(I thought multipart is the default.  Is that correct?)

<form id='idf_big' method="post" enctype="multipart/form-data">
   <textarea name="nta_big" id="idta_big" rows="10" cols="80">
      This is my textarea
   </textarea>
   <input type='submit' id='ids_big' value='Go'>
</form>

<script>
   jQuery(document).ready(function() {   
      $( "#idf_big" ).submit(function( event ) {
         event.preventDefault();
         var str = jQuery('#idf_big').serialize();
         var sos = str.length;
         alert("length of serialized data is: " + sos);
         var ph = jQuery.post('ajax_big_save.php', str);
         ...
  • Solution

The default is actually application/x-www-form-urlencoded. That's the normal key=value&key=value&... syntax you should recognize. multipart/form-data, mostly used for file uploads, is a far less compact format but better suited for lots of content.

 

Doing this as a fake file upload really sucks. Use the Blob and FormData classes.

var fd = new FormData();

var blob = new Blob([/* textarea contents */], { type: "text/plain" });
fd.append("nta_big", blob);

var xhr = new XMLHttpRequest();
xhr.open("POST", /* url */, true);
xhr.send(fd);
Untested but should be close.

 

With jQuery you might be able to use a Blob with its AJAX methods, as in

{ "nta_big": blob }
as part of the sent data.

YES!  It's so simple.... "I thought you'd say that Dr. Watson"


            var big = jQuery('#idta_big').val();
            
            var blob = new Blob ([big], { type: "text/plain" });

            var fd = new FormData();
            fd.append("bigstuff", blob);

            jQuery.ajax({
               url      : 'ajax_big_save.php.php',
               method   : 'POST',
               data     : fd
            });

THANK YOU!

 

(Stuff was in the $_FILES array.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.