Trying to upload an image and getting an error

I just tried this and it’s not working .

local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") -- forward declare the text fields local json = require("json") local button local MultipartFormData = require("class\_MultipartFormData") local userName = composer.getVariable( "username" ) local function networkListener( event ) if ( event.isError ) then local alert = native.showAlert( "Error Loading .", "Check your internet connection .", { "Try again" } ) end end local photo -- holds the photo object local PHOTO\_FUNCTION = media.PhotoLibrary -- or media.SavedPhotosAlbum? local tmpDirectory = system.TemporaryDirectory local photoGroup local localPhotoFileName= userName .. ".jpg" local btn\_selectFromGallery ----------------------------------------------------------------------- -- prepare the image for upload by saving it to the sandbox temporary directory ----------------------------------------------------------------------- local function prepImageForUpload() print( "saving"..localPhotoFileName ) --save the image to the local app sandbox display.save(photoGroup, localPhotoFileName, tmpDirectory) print( "pic saved in temp directory, uploading now" ) --finally upload the pic uploadPic()-- this would be your custom local function, needs work end ----------------------------------------------------------------------- -- Executes after the user selects a pic from the gallery (or cancels) ----------------------------------------------------------------------- local handleDoneSelectingPic= function(event) photo = event.target print("done selecting pic") if photo then print( "picSelected" ) photoGroup = display.newGroup() --this shows it on the screen too photoGroup:insert(photo) photoGroup:scale(.3,.3) photoGroup.x = display.contentWidth/2 photoGroup.y = display.contentWidth/2-100 localPhotoFileName="photoSelected"..math.random(100000, 999999)..".jpg" --save image after delay so that not in gallery picker and it has time to actually be rendered on the screen timer.performWithDelay( 500, prepImageForUpload ) else print( "No Image Selected" ) end end ----------------------------------------------------------------------- -- btn handler that opens the gallery ----------------------------------------------------------------------- local btn\_selectFromGallery local function selectFromGalleryBtn\_handler( event ) -- Delay a bit to allow the display to display refresh before calling the Photo Picker, then open it timer.performWithDelay( 100, function() media.selectPhoto( { listener = handleDoneSelectingPic, mediaSource = PHOTO\_FUNCTION } ) end ) return true end function scene:create(event) local screenGroup = self.view local background = display.newImageRect("insta.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local passedInParams = event.params --\<------ important. This is how you get the passed values default = display.newImage( "default.jpg" ) -- position the image default:translate( 160, 75 ) default:scale( 0.12, 0.12 ) screenGroup:insert(default) local userNameText = display.newText(userName, 160, 200, native.systemFont, 30 ) userNameText:setFillColor( 1, 0, 0 ) screenGroup:insert(userNameText) btn\_selectFromGallery = widget.newButton( { shape = "roundedRect", left = 70, top = 350, id = "pfp", label = "Upload picture", onEvent = pickPhoto, fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } }, labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } } } ) screenGroup:insert(btn\_selectFromGallery) end function scene:show(event) local phase = event.phase if ( phase == "will" ) then print("Phase started") elseif ( phase == "did" ) then print("phase on login") local tabBar = composer.getVariable("savedTabBar") tabBar.isVisible = true btn\_selectFromGallery:addEventListener("tap", selectFromGalleryBtn\_handler) end composer.removeScene( "login" ) end scene:addEventListener( "show" ) function scene:hide(event) local phase = event.phase if ( phase == "will" ) then print("Phase started") display.remove(button) elseif ( phase == "did" ) then print("phase on login") end end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene

I don’t see any where in that code where you have an “uploadPic()” function in this code and you’ve been sharing code that would appear to be that function. At some point you have to call code that sets up your multipart mime code and call network.request().

Your other code never shows where you call uploadPic() so there is some disconnect there. 

You also have your button with two handlers.

 btn\_selectFromGallery = widget.newButton( { label = "Upload picture", onEvent = pickPhoto, } )

Note, for clarity, I removed important style code for the button so you can focus in on the onEvent line. That’s trying to call a function called pickPhoto() which I also don’t see. You should not be adding your own events to widget buttons, in other words get rid of this line:

btn\_selectFromGallery:addEventListener("tap", selectFromGalleryBtn\_handler)

and change your button code to:

 btn\_selectFromGallery = widget.newButton( { shape = "roundedRect", left = 70, top = 350, id = "pfp", label = "Upload picture", onRelease = selectFromGalleryBtn\_handler, --\<------- change this line to this. fillColor = { default={ 1, 0.2, 0.5, 0.7 }, over={ 1, 0.2, 0.5, 1 } }, labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } } } )

But you still need to call your uploadPhoto() function which doesn’t seem to exist in the scene code above.

Rob

Yeah I was gonna add the uploadPic after I get the image in sandbox .