By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,365 Members | 3,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,365 IT Pros & Developers. It's quick & easy.

file upload with JSON+AJAX?

P: n/a
kj

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.
Jun 27 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
VK
On May 8, 12:14 am, kj <so...@987jk.com.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.
Jun 27 '08 #2

P: n/a
VK wrote:
On May 8, 12:14 am, kj <so...@987jk.com.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
Jun 27 '08 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
VK wrote:
>On May 8, 12:14 am, kj <so...@987jk.com.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>
Jun 27 '08 #4

P: n/a
kj
In <48************@PointedEars.deThomas 'PointedEars' Lahn <Po*********@web.dewrites:
>Thomas 'PointedEars' Lahn wrote:
>VK wrote:
>>On May 8, 12:14 am, kj <so...@987jk.com.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.
Jun 27 '08 #5

P: n/a
On May 8, 9:43 am, kj <so...@987jk.com.invalidwrote:
In <48222DD6.40...@PointedEars.deThomas 'PointedEars' Lahn <PointedE...@web.dewrites:
Thomas 'PointedEars' Lahn wrote:
VK wrote:
On May 8, 12:14 am, kj <so...@987jk.com.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.
Jun 27 '08 #6

P: n/a
slebetman wrote:
On May 8, 9:43 am, kj <so...@987jk.com.invalidwrote:
>In <48222DD6.40...@PointedEars.deThomas 'PointedEars' Lahn <PointedE...@web.dewrites:
>>Thomas 'PointedEars' Lahn wrote:
VK wrote:
On May 8, 12:14 am, kj <so...@987jk.com.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, <f8*******************@news.demon.co.uk>
Jun 27 '08 #7

P: n/a
On 7 May, 20:14, kj <so...@987jk.com.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...91d5edba814a5b
Jun 27 '08 #8

P: n/a
On 8 May, 10:49, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 7 May, 20:14, kj <so...@987jk.com.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
Jun 27 '08 #9

P: n/a
Captain Paralytic wrote:
On 8 May, 10:49, Captain Paralytic <paul_laut...@yahoo.comwrote:
>On 7 May, 20:14, kj <so...@987jk.com.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 bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #10

P: n/a
On 8 May, 14:34, Thomas 'PointedEars' Lahn <PointedE...@web.dewrote:
Captain Paralytic wrote:
On 8 May, 10:49, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 7 May, 20:14, kj <so...@987jk.com.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
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?
Jun 27 '08 #11

P: n/a
Captain Paralytic wrote:
On 8 May, 14:34, Thomas 'PointedEars' Lahn <PointedE...@web.dewrote:
>Captain Paralytic wrote:
>>On 8 May, 10:49, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 7 May, 20:14, kj <so...@987jk.com.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
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.