Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > file upload with JSON+AJAX?

Reply
Thread Tools

file upload with JSON+AJAX?

 
 
kj
Guest
Posts: n/a
 
      05-07-2008



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.
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      05-07-2008
On May 8, 12:14 am, kj <(E-Mail Removed)> wrote:
> 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.


 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-07-2008
VK wrote:
> On May 8, 12:14 am, kj <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-07-2008
Thomas 'PointedEars' Lahn wrote:
> VK wrote:
>> On May 8, 12:14 am, kj <(E-Mail Removed)> wrote:
>>> 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>
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      05-08-2008
In <(E-Mail Removed)> Thomas 'PointedEars' Lahn <(E-Mail Removed)> writes:

>Thomas 'PointedEars' Lahn wrote:
>> VK wrote:
>>> On May 8, 12:14 am, kj <(E-Mail Removed)> wrote:
>>>> 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.
 
Reply With Quote
 
slebetman
Guest
Posts: n/a
 
      05-08-2008
On May 8, 9:43 am, kj <(E-Mail Removed)> wrote:
> In <(E-Mail Removed)> Thomas 'PointedEars' Lahn <(E-Mail Removed)> writes:
>
> >Thomas 'PointedEars' Lahn wrote:
> >> VK wrote:
> >>> On May 8, 12:14 am, kj <(E-Mail Removed)> wrote:
> >>>> 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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-08-2008
slebetman wrote:
> On May 8, 9:43 am, kj <(E-Mail Removed)> wrote:
>> In <(E-Mail Removed)> Thomas 'PointedEars' Lahn <(E-Mail Removed)> writes:
>>> Thomas 'PointedEars' Lahn wrote:
>>>> VK wrote:
>>>>> On May 8, 12:14 am, kj <(E-Mail Removed)> wrote:
>>>>>> 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$(E-Mail Removed)>
 
Reply With Quote
 
Captain Paralytic
Guest
Posts: n/a
 
      05-08-2008
On 7 May, 20:14, kj <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Captain Paralytic
Guest
Posts: n/a
 
      05-08-2008
On 8 May, 10:49, Captain Paralytic <(E-Mail Removed)> wrote:
> On 7 May, 20:14, kj <(E-Mail Removed)> wrote:
>
> > 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-08-2008
Captain Paralytic wrote:
> On 8 May, 10:49, Captain Paralytic <(E-Mail Removed)> wrote:
>> On 7 May, 20:14, kj <(E-Mail Removed)> wrote:
>>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File upload from client application (non-form based upload) stuart@microsoft.com Python 1 11-25-2006 12:14 AM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM
How to detect sizeof upload in order to show status of browser file upload? Heather Fraser Java 0 07-05-2004 12:35 AM
Upload File - test for valid file type moondaddy ASP .Net 3 02-20-2004 05:58 PM
Upload Excel with Macros using HTML File Upload Prakash ASP General 3 11-12-2003 04:26 AM



Advertisments