How can I upload a video?

I have succeeded in uploading pictures to the server using the code that Rob suggested

https://forums.coronalabs.com/topic/70663-networkupload-and-matching-php-code/

Now I am trying to the same with a video that has been captured with media.captureVideo

I am using the example found under media.captureVideo()
https://docs.coronalabs.com/api/library/media/captureVideo.html

and just change the listener to send the URL of the video and what I think would be the directory

 local function onComplete( event ) if event.completed then -- media.playVideo( event.url, media.RemoteSource, true, onVideoComplete ) print( "event.duration: "..event.duration ) print( "event.fileSize: "..event.fileSize ) print("event.url: "..event.url) theVideoFile = event.url theDirectory = media.RemoteSource uploadVideo(theVideoFile,theDirectory,"testvideo.mov") end end

and in the uploadVideo I basically send this command:

local contentType = "multipart/form-data" network.upload( theUploadString , method, uploadListener, theVideoFile, theDirectory, contentType )

However the uploadListener never triggers so I am guessing that either it is not possible (this way) to upload a video or I need to have another value for “theDirectory”.

I have also tried to upload without having a value for the directory.

 local function uploadListener( event ) if ( event.isError ) then print( "Network Error." ) else if ( event.phase == "began" ) then print( "Upload started" ) elseif ( event.phase == "progress" ) then print( "Uploading... bytes transferred ", event.bytesTransferred ) elseif ( event.phase == "ended" ) then print( "Upload ended..." ) print( "Status:", event.status ) print( "Response:", event.response ) end end end

Are there anyone who has tried to solve this problem or could give me a hint in the right direction ?

The problem is that a URL is not a file, it’s a reference to a file.  I suspect you need to find a way to turn that URL into a path and use File IO operations to copy the video to say system.TemporaryDirectory so you can have a physical file to upload.

I’ve never tried this so I can’t offer how successful you will be with it. You might be able to search the forums to see if others have successfully done this.

Rob

Hi,

Does print(event.url) show a path by chance?

If so you could try system.pathForFile(event.url)

https://docs.coronalabs.com/api/library/system/pathForFile.html

I’m only taking a guess and have not tested it.

-dev

I have not solved it yet… any help is appreciated

The event.url get this value:

event.url: file:///private/var/mobile/Containers/Data/Application/407687C6-CE88-4621-9316-91B5A44A139C/tmp/53493873178\_\_40AAFBEB-A998-4887-AFDC-0FF1629B8DED.MOV

I have tried both

path = system.pathForFile(event.url) local file, errorString = io.open( path, "r" )

which gave the error

Error: bad argument #1 to 'open' (string expected, got nil)

I then tried

local path = event.url local file, errorString = io.open( path, "r" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) end

which gave the error 

File error: file:///private/var/mobile/Containers/Data/Application/407687C6-CE88-4621-9316-91B5A44A139C/tmp/53493873178\_\_40AAFBEB-A998-4887-AFDC-0FF1629B8DED.MOV: No such file or directory

I found the solution:

from the event.url, I have to read out what is the filename

local fileBegin = string.find(event.url,"/tmp/") local fileName = string.sub(event.url,fileBegin+5) print("fileName: "..fileName) print("fileBegin: "..fileBegin) path = system.pathForFile(fileName, system.TemporaryDirectory) uploadVideo(fileName, system.TemporaryDirectory,"testvideo.mov")

the uploadVideo routine is something like this:

local uploadVideo = function(theVideo,theDirectory,theNewName) local function uploadListener( event ) if ( event.isError ) then print( "Network Error." ) else if ( event.phase == "began" ) then print( "Upload started" ) elseif ( event.phase == "progress" ) then print( "Uploading... bytes transferred ", event.bytesTransferred ) elseif ( event.phase == "ended" ) then print( "Upload ended..." ) print( "Status:", event.status ) print( "Response:", event.response ) end end end local theUploadURL = ""http://www.theWebsite.com/upload.php?command=" local the\_Command = "upload" theUploadString = theUploadURL..the\_Command theUploadString = theUploadString.."&newfilename="..theNewName -- theUploadString = "http://www.theWebsite.com/upload.php?command=upload&filename=testvideo.mov" local method = "PUT" -- Set some reasonable parameters for the upload process: local params = { timeout = 60, progress = true, bodyType = "binary", field="file" } -- Specify what file to upload and where to upload it from. -- Also, set the MIME type of the file so that the server knows what to expect. local filename = theVideo local baseDirectory = theDirectory local contentType = "multipart/form-data" --another option is "text/plain" network.upload( theUploadString , method, uploadListener, params, filename, baseDirectory, contentType ) end

I’m glad you got it working!  Thanks for the solution.

Rob

The problem is that a URL is not a file, it’s a reference to a file.  I suspect you need to find a way to turn that URL into a path and use File IO operations to copy the video to say system.TemporaryDirectory so you can have a physical file to upload.

I’ve never tried this so I can’t offer how successful you will be with it. You might be able to search the forums to see if others have successfully done this.

Rob

Hi,

Does print(event.url) show a path by chance?

If so you could try system.pathForFile(event.url)

https://docs.coronalabs.com/api/library/system/pathForFile.html

I’m only taking a guess and have not tested it.

-dev

I have not solved it yet… any help is appreciated

The event.url get this value:

event.url: file:///private/var/mobile/Containers/Data/Application/407687C6-CE88-4621-9316-91B5A44A139C/tmp/53493873178\_\_40AAFBEB-A998-4887-AFDC-0FF1629B8DED.MOV

I have tried both

path = system.pathForFile(event.url) local file, errorString = io.open( path, "r" )

which gave the error

Error: bad argument #1 to 'open' (string expected, got nil)

I then tried

local path = event.url local file, errorString = io.open( path, "r" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) end

which gave the error 

File error: file:///private/var/mobile/Containers/Data/Application/407687C6-CE88-4621-9316-91B5A44A139C/tmp/53493873178\_\_40AAFBEB-A998-4887-AFDC-0FF1629B8DED.MOV: No such file or directory

I found the solution:

from the event.url, I have to read out what is the filename

local fileBegin = string.find(event.url,"/tmp/") local fileName = string.sub(event.url,fileBegin+5) print("fileName: "..fileName) print("fileBegin: "..fileBegin) path = system.pathForFile(fileName, system.TemporaryDirectory) uploadVideo(fileName, system.TemporaryDirectory,"testvideo.mov")

the uploadVideo routine is something like this:

local uploadVideo = function(theVideo,theDirectory,theNewName) local function uploadListener( event ) if ( event.isError ) then print( "Network Error." ) else if ( event.phase == "began" ) then print( "Upload started" ) elseif ( event.phase == "progress" ) then print( "Uploading... bytes transferred ", event.bytesTransferred ) elseif ( event.phase == "ended" ) then print( "Upload ended..." ) print( "Status:", event.status ) print( "Response:", event.response ) end end end local theUploadURL = ""http://www.theWebsite.com/upload.php?command=" local the\_Command = "upload" theUploadString = theUploadURL..the\_Command theUploadString = theUploadString.."&newfilename="..theNewName -- theUploadString = "http://www.theWebsite.com/upload.php?command=upload&filename=testvideo.mov" local method = "PUT" -- Set some reasonable parameters for the upload process: local params = { timeout = 60, progress = true, bodyType = "binary", field="file" } -- Specify what file to upload and where to upload it from. -- Also, set the MIME type of the file so that the server knows what to expect. local filename = theVideo local baseDirectory = theDirectory local contentType = "multipart/form-data" --another option is "text/plain" network.upload( theUploadString , method, uploadListener, params, filename, baseDirectory, contentType ) end

I’m glad you got it working!  Thanks for the solution.

Rob