network.upload() and applicationSuspend/applicationExit

Hello!

Need some help with network requests…

I try to upload photo from app to remote server, 

code something like that:

  1. get from db all photo, with status = ‘nosend’

  2. for every row, change status to ‘in send’ and send photo to server 

    network.upload( req , “POST”, networkListener, params, filename, system.DocumentsDirectory )

in listener

local function networkListener( event )              if event.isError then -- change status back to 'nosend'      else         if event.phase == "ended" then             if event.status == 200 then -- change status to 'send'         end         end      end return true end

in case app suspend and then back to screen - all ok, status change to ‘send’ or ‘nosend’ depending on the event

in case app suspend and then exit - status dont change, but photo may be delivered to server

My question: whether I can to check delivery for a server ?

Have a Corona team a instructions or a examples: network.requests with different app status (applicationSuspend/applicationExit)?

Thank for answering!

p.s.

sorry, my english is terrible

Uploading files is not easy. There are dozens of ways to upload files and they are all 100% dependent on the server’s expectations. Unless you know what the server’s expectations are, you can’t successfully upload files. The network.upload() is a helper function around network.request(), but it’s designed for a super-simple case of where the server is getting a stream of data and saving it. Many servers expect Multi-part MIME encoding. Others may want different HTTP headers included that say what the file type is. network.upload() is not a magical API.

This tutorial goes into more depth about this issue. https://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

As far as suspending/exiting goes, you only have a brief few cycles to do anything before the OS stops the app. The network.request API supports progress checks, but I’m not sure if it’s accurate enough to allow you to resume the file. The best you could do is cancel the download, assume it’s not there and try the upload when the app comes back.

Thanks Rob!

My problem:

when app suspend, then app again on screen - all ok

when app suspend, then user close app - request still is executed - photo save on server (im see photo on server side)

But in app I cant intercept this event, and again send photo - duplicate =(

If I understood correct, solution - cancel requst on exit app

Since i’m doing a few requst, i try to insert request id in table, like this:

local userDataId userDataId = network.upload( req , "POST", networkListener, params, filename, system.DocumentsDirectory ) myApp.networkReq[#myApp.networkReq + 1] = userDataId 

and in system event listener, i try to cancel requests:

if event.type == "applicationExit" then for i=1,#myApp.networkReq do network.cancel( myApp.networkReq[i] ) end

it is possible/correct ?

That sounds like it would work.

Rob

Uploading files is not easy. There are dozens of ways to upload files and they are all 100% dependent on the server’s expectations. Unless you know what the server’s expectations are, you can’t successfully upload files. The network.upload() is a helper function around network.request(), but it’s designed for a super-simple case of where the server is getting a stream of data and saving it. Many servers expect Multi-part MIME encoding. Others may want different HTTP headers included that say what the file type is. network.upload() is not a magical API.

This tutorial goes into more depth about this issue. https://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

As far as suspending/exiting goes, you only have a brief few cycles to do anything before the OS stops the app. The network.request API supports progress checks, but I’m not sure if it’s accurate enough to allow you to resume the file. The best you could do is cancel the download, assume it’s not there and try the upload when the app comes back.

Thanks Rob!

My problem:

when app suspend, then app again on screen - all ok

when app suspend, then user close app - request still is executed - photo save on server (im see photo on server side)

But in app I cant intercept this event, and again send photo - duplicate =(

If I understood correct, solution - cancel requst on exit app

Since i’m doing a few requst, i try to insert request id in table, like this:

local userDataId userDataId = network.upload( req , "POST", networkListener, params, filename, system.DocumentsDirectory ) myApp.networkReq[#myApp.networkReq + 1] = userDataId 

and in system event listener, i try to cancel requests:

if event.type == "applicationExit" then for i=1,#myApp.networkReq do network.cancel( myApp.networkReq[i] ) end

it is possible/correct ?

That sounds like it would work.

Rob