473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

file upload with JSON+AJAX?

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
11 8371
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kariach | last post by:
Hello, I am writing a web app that collects some user input and an optional image filename. When the user clicks submits, I use an asynchronous XML HTTP call to a backend asp.net handler to...
2
by: itsmesuraj | last post by:
Hi, I’m trying to create an application with AJAX which would upload a file on to the server. Am sending the request through the browser to the Servlet which uses the...
1
by: JagPeram | last post by:
Can Anyone tell me how to upload file using ajax with struts framework?
1
by: pritisolank | last post by:
Hello everyone, I am trying to do ajax file upload i am half way and now i don't find any way to reach :-( can someone tell me how to do a file upload in ajax+PHP. regards, priti
6
by: newlearner | last post by:
hi, im trying to upload a file using ajax and php. im not able to get the $FILES and $FILES. But when i try it out in the traditional format its works good. but when i try it out in ajax im...
1
by: kmithu | last post by:
Hi friends, I want to upload a file on AJAX web page. The HTML code i m using is: ---------------------------------------------------------------------------------------- <asp:ScriptManager...
5
dmjpro
by: dmjpro | last post by:
Hi Experts! I have been working with Ajax for there four months. But today suddenly come to my mind that if I want to upload the Image File using Ajax then what will be URL? Could some help me?...
3
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
1
by: kksandeep | last post by:
i am using this three files to uplod file. i got this file from net but i think these have some error. i am new to this field plz help the script i found is some helpful but not too that i need ...
6
vikysaran
by: vikysaran | last post by:
Hi, Please check the attachment http://www.golsinc.com/app7/AJAXEnabledWebSite.zip Here i made a small application for file upload. Made a huge form and need image upload without page refresh...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.