Jump to content

post form data (textarea) as file


fatkatie

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
Share on other sites

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);
         ...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.