Downloading to a custom directory

Hi @alberto1, @mroberti,

Are you both getting this warning in the Terminal/console?

“WARNING: invalid baseDirectory supplied, defaulting to system.DocumentsDirectory”

I’m getting the same results… that you can’t specify the subfolder in the “network.download” API. Can one of you please submit a bug report test + sample code? That will ensure it gets investigated sooner than later.

Thanks,

Brent

I am getting “Cannot create path for resource file <myfilename>. File does not exist.”

Brent if you wouldn’t mind checking this code for any obvious errors, I’ll submit it to the bug report section:

[lua]

local lfs = require “lfs”
– get raw path to app’s Temporary directory
local temp_path = system.pathForFile( “”, system.DocumentsDirectory )

– change current working directory
local success = lfs.chdir( temp_path ) – returns true on success
local new_folder_path
lfs.mkdir( “MyFolder” )

local function networkListener( event )
     if ( event.isError ) then
          print( “Network error - download failed” )
     elseif ( event.phase == “began” ) then
          print( “Progress Phase: began” )
     elseif ( event.phase == “ended” ) then
          print( “displaying response image file” )
          myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )
          myImage.alpha = 0
          transition.to( myImage, { alpha = 1.0 } )
     end
end

local params = {}
params.progress = true

local base = system.DocumentsDirectory
local path = system.pathForFile( “MyFolder/helloCopy.png”, base )

network.download(
http://developer.anscamobile.com/demo/hello.png”,
“GET”,
networkListener,
params,
“helloCopy.png”,
–path
system.DocumentsDirectory
)

[/lua]

I uncomment the “–path” part and comment out the “system.DocumentsDirectory” and thats when it crashes the simulator right after saying "Cannot create path for resource file ‘helloCopy.png’ b/c it does not exist.

Hi @mroberti,

I think I got it to work, actually, by prefixing the “fileName” parameter with the folder, as follows:

[lua]

local fileName = “myFile.txt”

network.download( “http://www.myWebsite.com//myFile.txt”, “GET”, downloadListener, params, “myFolder/”…fileName, system.DocumentsDirectory )

[/lua]

Can you test that out? Of course, it fails if you don’t have the folder already created using LFS…

Brent

Hi!

I think I’ve spelled wrong…
My code runs right now.

The network.download command is:

network.download( ruta\_catalogos..dname.."/"..arquivo, "GET", arqDescargado, "/"..id.."/"..arquivo, system.DocumentsDirectory )

In the way Brent says.

It runs fine for me (no errors, no warnings in the console)

Ahem…hello? Anyone? :wink:

How bout this. Using Corona’s own example from network.download: http://docs.coronalabs.com/api/library/network/download.html

How would I save that image into a custom directory?

Hello mroberti and Rob Miracle!

I use this code in Corona 2012.929 to download files directly to a subfolder, and everything runs fine:

local function baixaArquivos() local arqABaixar local docs\_path = system.pathForFile( "", system.DocumentsDirectory ) -- change current working directory local success = lfs.chdir( docs\_path ) local new\_folder\_path local dname = id if success then lfs.mkdir( dname ) new\_folder\_path = lfs.currentdir() .. "/" .. dname .. "/" end local resposta = loadValue("arquivos.data") local array\_arquivos = json.decode(resposta) local num\_arquivos = tonumber(table.maxn(array\_arquivos)) MbBaixados = 0 local function arqDescargado(event) local evento = event if ( evento.isError ) then local alert = native.showAlert( "Aviso", "Hay algún problema con la conexión a internet.\nNo se puede completar la operación.", { "Aceptar" },abortarDescarga) else if num\_arquivos \> 0 then if not descarga\_cancelada then local files\_path = system.pathForFile( id.."/"..array\_arquivos[num\_arquivos+1], system.DocumentsDirectory ) local tamano = lfs.attributes( files\_path, "size" ) MbBaixados = MbBaixados + tamano arqABaixar(array\_arquivos[num\_arquivos]) num\_arquivos = num\_arquivos - 1 else abortarDescarga() end elseif num\_arquivos == 0 then catalogoDescargado(catalogo\_a\_baixar) end end end function arqABaixar(arq) local arquivo = arq local destino = system.pathForFile("",system.DocumentsDirectory) network.download( ruta\_catalogos..dname.."/"..arquivo, "GET", arqDescargado, "/"..id.."/"..arquivo, destino ) end if num\_arquivos \> 0 then arqABaixar(array\_arquivos[num\_arquivos]) num\_arquivos = num\_arquivos - 1 end end

But today I download CoronaSDK-2013.1094 and get this error: Cannot create path for resource file ‘/3/0211605.jpg’. File does not exist.

It seems something has changed in any statment… but what?

Ok.

If I change this line:

network.download( ruta_catalogos…dname…"/"…arquivo, “GET”, arqDescargado, “/”…id…"/"…arquivo, destino )

at arqABaixar function, by:

network.download( ruta_catalogos…dname…"/"…arquivo, “GET”, arqDescargado, “/”…id…"/"…arquivo, system.DocumentsDirectory )

it runs.

I don’t know the reason…

Success!!!

[lua]

– Our folder name that we’d like to create

local folderName = “MyFolder”

local fileName = “thefile.png” – We only put png because below we’re donwloading a graphic from the web

local lfs = require “lfs”

– get raw path to app’s Documents directory, Temporary directory, wherever…

local temp_path = system.pathForFile( “”, system.DocumentsDirectory )

– change current working directory

local success = lfs.chdir( temp_path ) – returns true on success

– Create a folder named MyFolder

lfs.mkdir( folderName )

local function networkListener( event )

     if ( event.isError ) then

          print( “Network error - download failed” )

     elseif ( event.phase == “began” ) then

          print( “Progress Phase: began” )

     elseif ( event.phase == “ended” ) then

          print( “displaying response image file” )

          myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )

          myImage.alpha = 0

          transition.to( myImage, { alpha = 1.0 } )

     end

end

local params = {}

params.progress = true

– Above we created the directory in system.DocumentsDirectory, so we have

– to set that as the base here too.

local base = system.DocumentsDirectory

network.download( “http://developer.anscamobile.com/demo/hello.png”, “GET”, networkListener, params, folderName…"/"…fileName, base )

[/lua]

Thanks Rob and Brent and Alberto for helping us all work through it and figure it out!!  :smiley:

-Mario

okayguy.jpgOkay…

Am I doomed? Anyone? Should I file a bug report at this point?

Oops. Double posted!

Hi @alberto1, @mroberti,

Are you both getting this warning in the Terminal/console?

“WARNING: invalid baseDirectory supplied, defaulting to system.DocumentsDirectory”

I’m getting the same results… that you can’t specify the subfolder in the “network.download” API. Can one of you please submit a bug report test + sample code? That will ensure it gets investigated sooner than later.

Thanks,

Brent

I am getting “Cannot create path for resource file <myfilename>. File does not exist.”

Brent if you wouldn’t mind checking this code for any obvious errors, I’ll submit it to the bug report section:

[lua]

local lfs = require “lfs”
– get raw path to app’s Temporary directory
local temp_path = system.pathForFile( “”, system.DocumentsDirectory )

– change current working directory
local success = lfs.chdir( temp_path ) – returns true on success
local new_folder_path
lfs.mkdir( “MyFolder” )

local function networkListener( event )
     if ( event.isError ) then
          print( “Network error - download failed” )
     elseif ( event.phase == “began” ) then
          print( “Progress Phase: began” )
     elseif ( event.phase == “ended” ) then
          print( “displaying response image file” )
          myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )
          myImage.alpha = 0
          transition.to( myImage, { alpha = 1.0 } )
     end
end

local params = {}
params.progress = true

local base = system.DocumentsDirectory
local path = system.pathForFile( “MyFolder/helloCopy.png”, base )

network.download(
http://developer.anscamobile.com/demo/hello.png”,
“GET”,
networkListener,
params,
“helloCopy.png”,
–path
system.DocumentsDirectory
)

[/lua]

I uncomment the “–path” part and comment out the “system.DocumentsDirectory” and thats when it crashes the simulator right after saying "Cannot create path for resource file ‘helloCopy.png’ b/c it does not exist.

Hi @mroberti,

I think I got it to work, actually, by prefixing the “fileName” parameter with the folder, as follows:

[lua]

local fileName = “myFile.txt”

network.download( “http://www.myWebsite.com//myFile.txt”, “GET”, downloadListener, params, “myFolder/”…fileName, system.DocumentsDirectory )

[/lua]

Can you test that out? Of course, it fails if you don’t have the folder already created using LFS…

Brent

Hi!

I think I’ve spelled wrong…
My code runs right now.

The network.download command is:

network.download( ruta\_catalogos..dname.."/"..arquivo, "GET", arqDescargado, "/"..id.."/"..arquivo, system.DocumentsDirectory )

In the way Brent says.

It runs fine for me (no errors, no warnings in the console)

Success!!!

[lua]

– Our folder name that we’d like to create

local folderName = “MyFolder”

local fileName = “thefile.png” – We only put png because below we’re donwloading a graphic from the web

local lfs = require “lfs”

– get raw path to app’s Documents directory, Temporary directory, wherever…

local temp_path = system.pathForFile( “”, system.DocumentsDirectory )

– change current working directory

local success = lfs.chdir( temp_path ) – returns true on success

– Create a folder named MyFolder

lfs.mkdir( folderName )

local function networkListener( event )

     if ( event.isError ) then

          print( “Network error - download failed” )

     elseif ( event.phase == “began” ) then

          print( “Progress Phase: began” )

     elseif ( event.phase == “ended” ) then

          print( “displaying response image file” )

          myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )

          myImage.alpha = 0

          transition.to( myImage, { alpha = 1.0 } )

     end

end

local params = {}

params.progress = true

– Above we created the directory in system.DocumentsDirectory, so we have

– to set that as the base here too.

local base = system.DocumentsDirectory

network.download( “http://developer.anscamobile.com/demo/hello.png”, “GET”, networkListener, params, folderName…"/"…fileName, base )

[/lua]

Thanks Rob and Brent and Alberto for helping us all work through it and figure it out!!  :smiley:

-Mario