Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

multipart/form-data

 [1/5] from: jason:cunliffe:verizon at: 30-Aug-2002 8:04


Andreas, you kindly posted a multipart/form-data script for uploading files via HTTP with some instructions in a later email. Alas I am struggling still to get HTTP upload working smoothly in REBOL. Your solution is now __almost__ working for me. But man I feel so dumb :-( Some simple basic cgi object thang I am not grokking yet. I hope something can help me. I really want to get this cooking with Vanilla :-) 1. I saved the script as %upload-andreas.r [attached] 2. Called by a web form: <form method="post" action="upload-andreas.r" enctype="multipart/form-data" /> <b>upload-andreas.r</b> <p>Select an image or script file to upload </p> <fieldset> <legend>File to upload:<br /></legend> <input type="hidden" name="MAX_SIZE" value="100000" /><br/> Enter filename: <input type="file" name="filename" size="50" /><br/> <input type="submit" value="upload" background-color: #FFFF00 /><br/> </fieldset> </form> <p> 3. Then as you advised.. ;Friday, August 23, 2002, 12:38:09 AM, Jason wrote: ;> Can you help me understand how to use it? ; Andreas => First you fetch the post-data (the HTTP POST entity body, to be correct) as always len: load any [ system/options/cgi/content-length "0"] post-data: make string! (len + 10) while [len > 0][ len: len - read-io system/ports/input post-data len ] ; Then this POST-data can be handed over to decode-multipart-form-data ; (if it is encoded according to the mime multipart RFC's as ; indicated by a content type beeing multipart/form-data) tmp-post: either found? find any [ system/options/cgi/content-type "" ] ["multipart/form-data"] [ decode-multipart-form-data system/options/cgi/content-type post-data ] [ decode-cgi post-data ] ^^^^ This did not work :-( ** Script Error: tmp-post is missing its p-ct-dispo argument ** Where: do-boot ** Near: tmp-post Q1: Why? So I just tried a very minimal version: A. tmp-post: decode-cgi post-data ok no compaints. But when I try this: B. tmp-post: decode-multipart-form-data system/options/cgi/content-type post-data I keep getting the same Script Error as above. So I stuck with the basic decode-cgi post-data version (A.) write %test.jpg tmp-post print rejoin [{<a href="test.jpg">tmp-post</a>}] 4.Select a jpeg file to upload from my form, et voila -> when I click on the tmp-post link I can see the form headers and content of my Jpeg images as acii like this: -----------------------------7d22e4fe68 Content-Disposition: form-data; name"MAX_SIZE" 100000 -----------------------------7d22e4fe68 Content-Disposition: form-data; name="description" describe me -----------------------------7d22e4fe68 Content-Disposition: form-data; name="filename"; filename="E:\FLASH\jasonMX\buddha.jpg" Content-Type: image/pjpeg JFIFHH File written by Adobe Photoshop 5.2 .........etc etc etc............ Q2: How to I access the tmp-post object? For example the filename or description? Q3: How to save just the image contents as a jpeg file, and link it for display. Thanks very much for any help to crack this nut ./Jason -- Attached file included as plaintext by Listar -- -- File: upload-andreas.r #!/home/rebol/rebol -cs REBOL [ Title: "decode-multipart-form-data" Authors: ["Andreas Bolka" [andreas--bolka--gmx--net]] testing: [ "Jason" [jasonic--nomadics--org] 2002-08-30] Date: 2002-06-18 Version: 1.1 Purpose: { Decodes POST-data encoded as "multipart/form-data" as defined by RFC 2388. The output is compatible to 'decode-cgi wherever possible. So the output contains a list of set-word's and values, one pair for each data field. example: [ field1: "foo" field2: "bar" ] If the content type of one part of the form-data is not equal to text/plain, then the value of the respective mime-part is an object! with the following attribs: filename, type, content. example: [ field1: "foo" field2: make object! [ filename: "user.xml" type: "text/xml" content: "" ] ] } ] decode-multipart-form-data: func [ p-content-type p-post-data /local list ct pd bd delim-beg delim-end mime-part ] [ list: copy [] if not found? find p-content-type "multipart/form-data" [ return list ] ct: copy p-content-type pd: copy p-post-data bd: join "--" copy find/tail ct "boundary=" delim-beg: join crlf crlf delim-end: rejoin [ crlf bd ] mime-part: [ ( ct-dispo: content: none ct-type: "text/plain" ) thru bd thru "content-disposition: " copy ct-dispo to crlf opt [ thru "content-type: " copy ct-type to crlf ] thru delim-beg copy content to delim-end ( handle-mime-part ct-dispo ct-type content ) ] handle-mime-part: func [ p-ct-dispo p-ct-type p-content /local fieldname ] [ p-ct-dispo: parse p-ct-dispo [describe ;=" here] fieldname: select p-ct-dispo "name" append list to-set-word fieldname either found? find p-ct-type "text/plain" [append list content][ append list make object! [ filename: select p-ct-dispo "filename" type: copy p-ct-type content: either none? p-content [ none ] [ copy p-content ] ] ] ] use [ ct-dispo ct-type content ] [ parse/all pd [ some mime-part ] ] return list ] ] ;Friday, August 23, 2002, 12:38:09 AM, Jason wrote: ;> Can you help me understand how to use it? ; Andreas => First you fetch the post-data (the HTTP POST entity body, to be correct) as always len: load any [ system/options/cgi/content-length "0"] post-data: make string! (len + 10) while [len > 0][ len: len - read-io system/ports/input post-data len ] ; Then this POST-data can be handed over to decode-multipart-form-data ; (if it is encoded according to the mime multipart RFC's as ; indicated by a content type beeing multipart/form-data) ;;** Script Error: tmp-post is missing its p-ct-dispo argument ** Where: do-boot ** Near: tmp-post ;tmp-post: either found? find any [ system/options/cgi/content-type "" ] ["multipart/form-data"] ;[ decode-multipart-form-data system/options/cgi/content-type post-data ] ;[ decode-cgi post-data ] tmp-post: decode-cgi post-data ;tmp-post: decode-multipart-form-data system/options/cgi/content-type post-data ;After that you'll have a decode-cgi like structure in tmp-post which ;is described in full detail in the headers of the script i posted before. print "Content-Type: text/html^/" ;print "Content-Type: image/jpeg^/" print "hello from %upload-andreas.r" write %test.jpg tmp-post print rejoin [{<a href="test.jpg">tmp-post</a>}]

 [2/5] from: andreas:bolka:gmx at: 30-Aug-2002 19:22


------ comment [ ; for Jason only When you store some REBOL code into a vanilla snip, look out for {}'s in the original code, as these are valid string delimiters in REBOL but special characters in vanilla. Use my original code as posted on the mailing list as you've one of these "escpaing mistakes" in the file you attached to your last mail. ] ------ In your example, tmp-post will look like the following: [ MAX_SIZE: "100000" description: "describe me" filename: make object! [ filename: "E:\FLASH\jasonMX\buddha.jpg" type: "image/pjpeg" content: {JFIFHH File ....} ] ] you'd probably use it as follows: cgi-obj: construct tmp-post or if you use some older rebol cgi-obj: make object! tmp-post and then access the filename of the uploaded file via cgi-obj/filename/filename or you could write the content to a file write/binary %somefile.jpg cgi-obj/filename/content all of these should work :) good luck! :) Friday, August 30, 2002, 2:04:01 PM, Jason wrote:
> -----------------------------7d22e4fe68 > Content-Disposition: form-data; name"MAX_SIZE"
<<quoted lines omitted: 12>>
> Q3: How to save just the image contents as a jpeg file, and link it > for display.
-- Best regards, Andreas mailto:[andreas--bolka--gmx--net]

 [3/5] from: g:santilli:tiscalinet:it at: 30-Aug-2002 20:01


Hi Andreas, On Friday, August 30, 2002, 7:22:32 PM, you wrote: AB> When you store some REBOL code into a vanilla snip, look out for {}'s AB> in the original code, as these are valid string delimiters in REBOL AB> but special characters in vanilla. BTW Andres and Chris, what about using something different, or at least allowing the user to disable this kind of processing for some snips? This is mainly a problem if you want to include CSS styles in a template. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [4/5] from: andreas:bolka:gmx at: 31-Aug-2002 14:19


Friday, August 30, 2002, 8:01:27 PM, Gabriele wrote:
> On Friday, August 30, 2002, 7:22:32 PM, you wrote: >> When you store some REBOL code into a vanilla snip, look out for
<<quoted lines omitted: 4>>
> snips? This is mainly a problem if you want to include CSS styles in > a template.
A method to partially disable text parsing is already designed, if you'd like to implement it, contact me off-list. And if you want to use CSS stylesheets right now, use external stylesheets and include them in your template. -- Best regards, Andreas mailto:[andreas--bolka--gmx--net]

 [5/5] from: g:santilli:tiscalinet:it at: 31-Aug-2002 16:02


Hi Andreas, On Saturday, August 31, 2002, 2:19:35 PM, you wrote: AB> And if you want to use CSS stylesheets right now, use external AB> stylesheets and include them in your template. I know, but it would be nice to have CSS files as snips too. I think that simply storing a "content type" metadata for the snips and then acting based on it would solve all the problems and open new possibilities. For example "vanilla/snip" snips would be processed the usual way, "vanilla/dynasnip" could be executed (maybe with restrictions), "text/html" or "text/css" simply sent to the browser... I'm also thinking of something like text/x-etext that would be processed by Andrew's eText, and so on... Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted