file upload with JSON+AJAX?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kj

    file upload with JSON+AJAX?




    I would like to convert a form that currently uses the traditional
    CGI sequence (i.e. the action associated with the form sends a POST
    request to a server-side CGI script) to one that uses AJAX to send
    a JSON-encoded request to a remote web service.

    The stumbling block I'm running into is that one of the inputs in
    the form is a file upload element, and I can't figure out how to
    include the data from the specified file in the JSON payload. Is
    there a way to do this?

    TIA!

    Kynn
    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
  • VK

    #2
    Re: file upload with JSON+AJAX?

    On May 8, 12:14 am, kj <so...@987jk.co m.invalidwrote:
    I can't figure out how to
    include the data from the specified file in the JSON payload. Is
    there a way to do this?
    No. XHR doesn't allow local file access, so for the file upload you
    have to use traditional forms.


    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: file upload with JSON+AJAX?

      VK wrote:
      On May 8, 12:14 am, kj <so...@987jk.co m.invalidwrote:
      >I can't figure out how to
      >include the data from the specified file in the JSON payload. Is
      >there a way to do this?
      >
      No. XHR doesn't allow local file access,
      It does when running locally.
      so for the file upload you have to use traditional forms.
      Correct.


      PointedEars
      --
      realism: HTML 4.01 Strict
      evangelism: XHTML 1.0 Strict
      madness: XHTML 1.1 as application/xhtml+xml
      -- Bjoern Hoehrmann

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: file upload with JSON+AJAX?

        Thomas 'PointedEars' Lahn wrote:
        VK wrote:
        >On May 8, 12:14 am, kj <so...@987jk.co m.invalidwrote:
        >>I can't figure out how to
        >>include the data from the specified file in the JSON payload. Is
        >>there a way to do this?
        >No. XHR doesn't allow local file access,
        >
        It does when running locally.
        It also does when running via HTTP, and the required privileges are granted,
        or when running out of the sandbox.

        PointedEars
        --
        Use any version of Microsoft Frontpage to create your site.
        (This won't prevent people from viewing your source, but no one
        will want to steal it.)
        -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

        Comment

        • kj

          #5
          Re: file upload with JSON+AJAX?

          In <48222DD6.40003 @PointedEars.de Thomas 'PointedEars' Lahn <PointedEars@we b.dewrites:
          >Thomas 'PointedEars' Lahn wrote:
          >VK wrote:
          >>On May 8, 12:14 am, kj <so...@987jk.co m.invalidwrote:
          >>>I can't figure out how to
          >>>include the data from the specified file in the JSON payload. Is
          >>>there a way to do this?
          >>No. XHR doesn't allow local file access,
          >>
          >It does when running locally.
          >It also does when running via HTTP, and the required privileges are granted,
          >or when running out of the sandbox.
          I'm not familiar with either of these two possibilities. I'm
          particularly intrigued by the first one. How can a web app request
          the necessary privileges?

          Thanks in advance!

          Kynn

          --
          NOTE: In my address everything before the first period is backwards;
          and the last period, and everything after it, should be discarded.

          Comment

          • slebetman

            #6
            Re: file upload with JSON+AJAX?

            On May 8, 9:43 am, kj <so...@987jk.co m.invalidwrote:
            In <48222DD6.40... @PointedEars.de Thomas 'PointedEars' Lahn <PointedE...@we b.dewrites:
            >
            Thomas 'PointedEars' Lahn wrote:
            VK wrote:
            >On May 8, 12:14 am, kj <so...@987jk.co m.invalidwrote:
            >>I can't figure out how to
            >>include the data from the specified file in the JSON payload. Is
            >>there a way to do this?
            >No. XHR doesn't allow local file access,
            >
            It does when running locally.
            It also does when running via HTTP, and the required privileges are granted,
            or when running out of the sandbox.
            >
            I'm not familiar with either of these two possibilities. I'm
            particularly intrigued by the first one. How can a web app request
            the necessary privileges?
            >
            A web app can't programatically request the necessary privileges. The
            user needs to either set preferences in his browser or grant
            premission to do it. Obviously not all browsers have a let-really-
            dangerous-code-access-my-harddisk preference to set. I believe you can
            enable such a feature in firefox using about:config.

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: file upload with JSON+AJAX?

              slebetman wrote:
              On May 8, 9:43 am, kj <so...@987jk.co m.invalidwrote:
              >In <48222DD6.40... @PointedEars.de Thomas 'PointedEars' Lahn <PointedE...@we b.dewrites:
              >>Thomas 'PointedEars' Lahn wrote:
              >>>VK wrote:
              >>>>On May 8, 12:14 am, kj <so...@987jk.co m.invalidwrote:
              >>>>>I can't figure out how to
              >>>>>include the data from the specified file in the JSON payload. Is
              >>>>>there a way to do this?
              >>>>No. XHR doesn't allow local file access,
              >>>It does when running locally.
              >>It also does when running via HTTP, and the required privileges are granted,
              >>or when running out of the sandbox.
              >I'm not familiar with either of these two possibilities. I'm
              >particularly intrigued by the first one. How can a web app request
              >the necessary privileges?
              >
              A web app can't programatically request the necessary privileges.
              Yes, it can. I have posted a proof-of-concept here before.
              The user needs to either set preferences in his browser or grant
              premission to do it.
              As for the latter, that is why it is called a privilege *request*.


              PointedEars
              --
              Prototype.js was written by people who don't know javascript for people
              who don't know javascript. People who don't know javascript are not
              the best source of advice on designing systems that use javascript.
              -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

              Comment

              • Captain Paralytic

                #8
                Re: file upload with JSON+AJAX?

                On 7 May, 20:14, kj <so...@987jk.co m.invalidwrote:
                The stumbling block I'm running into is that one of the inputs in
                the form is a file upload element, and I can't figure out how to
                include the data from the specified file in the JSON payload. Is
                there a way to do this?
                There is a way to achieve the effect. I pointed out in a recent
                discussion on this forum that Google Mail performs an AJAX type upload
                of attachments. If you search back for the discussion, I think you'll
                find that the solution had something to do with using an iframe.


                Comment

                • Captain Paralytic

                  #9
                  Re: file upload with JSON+AJAX?

                  On 8 May, 10:49, Captain Paralytic <paul_laut...@y ahoo.comwrote:
                  On 7 May, 20:14, kj <so...@987jk.co m.invalidwrote:
                  >
                  The stumbling block I'm running into is that one of the inputs in
                  the form is a file upload element, and I can't figure out how to
                  include the data from the specified file in the JSON payload. Is
                  there a way to do this?
                  >
                  There is a way to achieve the effect. I pointed out in a recent
                  discussion on this forum that Google Mail performs an AJAX type upload
                  of attachments. If you search back for the discussion, I think you'll
                  find that the solution had something to do with using an iframe.
                  >
                  http://groups.google.co.uk/group/com...rowse_frm/thre...
                  Here is sample code:

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: file upload with JSON+AJAX?

                    Captain Paralytic wrote:
                    On 8 May, 10:49, Captain Paralytic <paul_laut...@y ahoo.comwrote:
                    >On 7 May, 20:14, kj <so...@987jk.co m.invalidwrote:
                    >>The stumbling block I'm running into is that one of the inputs in
                    >>the form is a file upload element, and I can't figure out how to
                    >>include the data from the specified file in the JSON payload. Is
                    >>there a way to do this?
                    >There is a way to achieve the effect. I pointed out in a recent
                    >discussion on this forum that Google Mail performs an AJAX type upload
                    >of attachments. If you search back for the discussion, I think you'll
                    >find that the solution had something to do with using an iframe.
                    >>
                    >http://groups.google.co.uk/group/com...rowse_frm/thre...
                    >
                    Here is sample code:
                    http://www.ajaxonomy.com/2008/ajax/u...asynchronously
                    While it certainly is possible, although not generally feasible, to upload
                    local files *through XHR*, on a closer look you will realize that this
                    solution is _not_ what was being asked for. Instead, it implements the
                    already proposed solutions of using a (static or dynamically generated;
                    here the former) HTML form to submit the information. The only real
                    difference to a non-scripted form is that the form is submitted with
                    scripting.

                    The very resource you have referred to correctly confirms that:

                    | How to upload files asynchronously?
                    |
                    | As I described above You can’t upload files using AJAX. Some time ago I
                    | wrote a JavaScript file upload object which as You already understood can
                    | help developers make forms which upload files asynchronously. [...]


                    PointedEars
                    --
                    var bugRiddenCrashP ronePieceOfJunk = (
                    navigator.userA gent.indexOf('M SIE 5') != -1
                    && navigator.userA gent.indexOf('M ac') != -1
                    ) // Plone, register_functi on.js:16

                    Comment

                    • Captain Paralytic

                      #11
                      Re: file upload with JSON+AJAX?

                      On 8 May, 14:34, Thomas 'PointedEars' Lahn <PointedE...@we b.dewrote:
                      Captain Paralytic wrote:
                      On 8 May, 10:49, Captain Paralytic <paul_laut...@y ahoo.comwrote:
                      On 7 May, 20:14, kj <so...@987jk.co m.invalidwrote:
                      >The stumbling block I'm running into is that one of the inputs in
                      >the form is a file upload element, and I can't figure out how to
                      >include the data from the specified file in the JSON payload. Is
                      >there a way to do this?
                      There is a way to achieve the effect. I pointed out in a recent
                      discussion on this forum that Google Mail performs an AJAX type upload
                      of attachments. If you search back for the discussion, I think you'll
                      find that the solution had something to do with using an iframe.
                      >>>
                      While it certainly is possible, although not generally feasible, to upload
                      local files *through XHR*, on a closer look you will realize that this
                      solution is _not_ what was being asked for. Instead, it implements the
                      already proposed solutions of using a (static or dynamically generated;
                      here the former) HTML form to submit the information. The only real
                      difference to a non-scripted form is that the form is submitted with
                      scripting.
                      >
                      The very resource you have referred to correctly confirms that:
                      >
                      | How to upload files asynchronously?
                      |
                      | As I described above You can’t upload files using AJAX. Some time ago I
                      | wrote a JavaScript file upload object which as You already understood can
                      | help developers make forms which upload files asynchronously. [...]
                      >
                      PointedEars
                      And I very carefully worded my post as "There is a way to achieve the
                      effect.", acknowledging that one got the "effect" of an AJAX upload,
                      but not a real AJAX upload.

                      So what's your problem?

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: file upload with JSON+AJAX?

                        Captain Paralytic wrote:
                        On 8 May, 14:34, Thomas 'PointedEars' Lahn <PointedE...@we b.dewrote:
                        >Captain Paralytic wrote:
                        >>On 8 May, 10:49, Captain Paralytic <paul_laut...@y ahoo.comwrote:
                        >>>On 7 May, 20:14, kj <so...@987jk.co m.invalidwrote:
                        >>>>The stumbling block I'm running into is that one of the inputs in
                        >>>>the form is a file upload element, and I can't figure out how to
                        ^^^^^^
                        >>>>include the data from the specified file in the JSON payload. Is
                        ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^
                        >>>>there a way to do this?
                        ^^^^^^^^^^^^^^^ ^^^^^^^^
                        >>>There is a way to achieve the effect. I pointed out in a recent
                        ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^
                        >>>discussion on this forum that Google Mail performs an AJAX type upload
                        >>>of attachments. If you search back for the discussion, I think you'll
                        >>>find that the solution had something to do with using an iframe.
                        >>>http://groups.google.co.uk/group/com...rowse_frm/thre...
                        >>Here is sample code:
                        >>http://www.ajaxonomy.com/2008/ajax/u...asynchronously
                        >While it certainly is possible, although not generally feasible, to upload
                        >local files *through XHR*, on a closer look you will realize that this
                        >solution is _not_ what was being asked for. [...]
                        >
                        And I very carefully worded my post as "There is a way to achieve the
                        effect.", acknowledging that one got the "effect" of an AJAX upload,
                        but not a real AJAX upload.
                        >
                        So what's your problem?
                        Apparently your "effect" statement in relation to the stated requirement was
                        too ambiguous to be left alone, to say the least. For the desired effect
                        was to have the file content included *"in the (submitted) JSON payload"*,
                        which cannot be achieved with a file input control.


                        PointedEars
                        --
                        Anyone who slaps a 'this page is best viewed with Browser X' label on
                        a Web page appears to be yearning for the bad old days, before the Web,
                        when you had very little chance of reading a document written on another
                        computer, another word processor, or another network. -- Tim Berners-Lee

                        Comment

                        Working...