I recently re-written the offline code to get rid of the deprecated
code and use the desktop, blob and httpRequest classes (old code was
written before the code was deprecated).
What I did (with steps):

1. In all my form where was an input type file element, I added js
code that will check if the app is in offline mode and replace that
html (input type file) with a normal button that have onclick
desktop.openFiles(this.openFilesCallback, {singleFile: true});. I use
singleFile true to be exactly like the online upload (for user there
is no diference);

2. I have openFilesCallback that will call another function:
storeAttachment(files[0].blob, files[0].name); (because I use
singleFile: true, I only have files[0] in array)

3. In storeAttachment function, I am storing the file references into
db with name (that I get as a parameter) and I get an id for that
entry. I am generating an url where the file can be saw in offline
mode (before sync with the online server). This url should be unique,
so I use the db id for that: url = '/attachments/' + db_id + '/' +
fileName; Then I store the content of the file in store:
store.captureBlob(blob, url);

4. Update the db details of the files (I need those details later)
with size and content type:  size = parseInt(store.getHeader(url,
'Content-Length')), contentType = store.getHeader(url, 'Content-
Type'),

5. At this point I have the content of the file, an url where the file
is available and a reference for the file stored in DB. All files
uploaded while in offline mode are working, only sync with server
remains to be done.

6. For sync I am using HttpRequest class and not an AJAX post because
I would need to encode the binary content of files and that could be
very complicated.

7. I simply iterate from the table with offline uploaded files and for
each record there I have the name, size, contentType and url. I get
the content with store.getAsBlob(url).

8. request.open('POST', url_of_my_site_where_I_handle_the_request);
    request.onreadystatechange = function() {
            if (request.readyState == 4) {
                eval(request.responseText);
            }
        };
        request.send(blob);

9. I have on the url where I do the post, a get parameter to tell that
this is an upload that is from offline (not the normal form), so I
know that I have to "work" a bit before I have the file in same form
as it is when is uploaded from input type file element. This is why I
have size and content type stored before.


Hope that helps

On Apr 23, 2:15 am, Diogenes Duarte <[email protected]> wrote:
> I'm here again hoping for some help.
>
> Reading the gears docs, I saw a method called captureFile from ResourceStore
> class. it fits right where I need. I have an app that the users fill an form
> with some data, and they can also put some files in <input type='files'>.
> So, gears captures the file with the method captureFile and when the machine
> has internet connection, the app creates a form with some <input
> type='file'> and using the method of FileSubmitter it fills the inputs and
> after sends the request with the form created. It works fine, but I'm using
> depracated methods and they only works under I.E and Firefox. I wanna change
> this because now, the users are going to use Chrome.
>
> The problem is that I don't see clearly how can I do what I need for another
> way. I read about the method openFile from desktop and I think I can get the
> file contents and put it on sqlite, but the problem is, how can I send it to
> the server after connection is established? My code on server side expects
> file inputs. How can I fill them if I can't use the depracated methods? and
> if I send using HttpRequest.send() by posting the contents of file in
> postData, how will I get the data passed on server side? I didn't see this
> on docs. Perhaps an example of using HttpRequest.send to send files to a web
> app can make me see what I need to do.
>
> thanks for attention and sorry bad english.

Reply via email to