Uploading Files Using Network.upload

Hello Everyone,

i posted something similar 8 months ago… but i have a few updates to what i know now:

i have a webservice created with C#, it allows for uploading files

i used a tool like Fiddler to test it, it is working perfectly

using Corona the code is working but the files are not being uploaded

so i ran the webservice in debug mode and here are the main differences between Corona and Fiddler:

  • File size sent by Fiddler is slightly larger than Corona’s for the same file, i think this is because Fiddler sends the headers and Body size and everything passed through http, while Corona is sending the exact bytes of file size only, as seen by using Windows-Properties-Menu for that file
  • Headers passed by fiddler does not have the value named  Keep-Alive and this value is always passed by corona automatically not by me, and i don’t know where is it coming from or what it is … the first header value passed by fiddler is the file size, in corona file size is the second value
  • Corona Help suggest using Content-Type to be “image/jpeg” while Fiddler sends this value “multipart/form-data” even after trying to use this value in corona, no error occurs but no successful uploading
  • in Fiddler there’s something called boundary as well … below is Fiddlers Header output

Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468

User-Agent: Fiddler

Host: localhost:6991

Content-Length: 2645

and Fiddlers Request Body

---------------------------acebdf13572468

Content-Disposition: form-data; name=“fieldNameHere”; filename="abc.jpg"

Content-Type: image/jpeg

<@INCLUDE *D:\Lua\Projects\WSUpload\images\abc.jpg*@>

---------------------------acebdf13572468–

in Corona i used both network.upload, and network.request … the webservice is being called, all headers and parameters are showing, but Request.Files.Count=0 from .Net Code which returns one  Request.Files.Count=1 in Fiddlers case

the main difference is that in debug mode in .Net>>>> for Fiddler

Request.Headers(0)=2645

which is the file size

>>>>and for Corona

Request.Headers(0)=Keep-Alive

and 

Request.Headers(1)=2453

i’m sure my whole problem is in setting up headers , and body properly… although corona help does not mention setting up the body unless you are using network.request for the upload process

so mainly what is

  • Keep-Alive and how to get rid of it
  • use multipart/form-data or  image/jpeg
  • and why file size is different

i really need to get this working, if it is a problem between .Net and Corona then i can learn a few PHP lines of code and create the service because i have pending projects because of this feature

Please Help anyone…

Regards,

Tariq

The network.upload() API simply dumps the image to the server. It will not work with scripts expecting multipart data. This tutorial will help some, but since  you’re using .NET you will have to figure out the differences.

https://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

Rob

Thanks Rob,

i went through that particular article thoroughly, but couldn’t solve my problem, i think i tried everything and almost all possibilities… and i’m almost giving up :slight_smile:

what is making it hard, is that Corona is not giving errors, and .Net is not giving errors

and the listener in Corona is saying that bytes are being sent… i even tried a 700MB file … it works smoothly … and the listener keeps printing updated sent bytes…

but on .NET side… no files are received … not only in expected folder … but in debug mode … and the code says there are no files sent … using Fiddler it is working!!

so i think that .NET expects the first header value to be file size, just like Fiddler is providing, instead it is getting that  Keep-Alive value  Which is stupid from .NET if it was that way… because they should rely on Tag Title and Tag Value … not expect values to be ordered in a certain way!!

could it be that it does not work on simulator on Windows?!

i tried it on phone … but i cannot just keep compiling each possibility and test on phone…

If your script expects multi-part mime, that’s what you’re going to have to provide it and use network.request() instead of network.upload().

This might be useful:

https://github.com/breinhart/Corona-SDK-Tweet-Media/blob/master/utils/multipartForm.lua

Rob

Hi Rob,

i tried the recent thing you sent me, and believe it or not, exact same problem, i think the problem is in structuring the header and body part of the sent files … one wrong character will make everything fail …

**:)**the Great part is i downloaded the PHP sample file from the demystified article for network.upload, and without knowing anything about php it worked immediately …

so my advise for everyone, if you want to upload files through Corona, use the PHP file as a webservice … you can download it from this link

https://www.dropbox.com/s/yc3z9a9xgf5kkim/upload.php

follow the setup instruction like security, and folders to be created along with the php file on your hosting server

and in corona use code from this page

https://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

but use POST not PUT

and it will work like magic

Finally … i have been searching for months to solve this :-) 

The network.upload() API simply dumps the image to the server. It will not work with scripts expecting multipart data. This tutorial will help some, but since  you’re using .NET you will have to figure out the differences.

https://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

Rob

Thanks Rob,

i went through that particular article thoroughly, but couldn’t solve my problem, i think i tried everything and almost all possibilities… and i’m almost giving up :slight_smile:

what is making it hard, is that Corona is not giving errors, and .Net is not giving errors

and the listener in Corona is saying that bytes are being sent… i even tried a 700MB file … it works smoothly … and the listener keeps printing updated sent bytes…

but on .NET side… no files are received … not only in expected folder … but in debug mode … and the code says there are no files sent … using Fiddler it is working!!

so i think that .NET expects the first header value to be file size, just like Fiddler is providing, instead it is getting that  Keep-Alive value  Which is stupid from .NET if it was that way… because they should rely on Tag Title and Tag Value … not expect values to be ordered in a certain way!!

could it be that it does not work on simulator on Windows?!

i tried it on phone … but i cannot just keep compiling each possibility and test on phone…

If your script expects multi-part mime, that’s what you’re going to have to provide it and use network.request() instead of network.upload().

This might be useful:

https://github.com/breinhart/Corona-SDK-Tweet-Media/blob/master/utils/multipartForm.lua

Rob

Hi Rob,

i tried the recent thing you sent me, and believe it or not, exact same problem, i think the problem is in structuring the header and body part of the sent files … one wrong character will make everything fail …

**:)**the Great part is i downloaded the PHP sample file from the demystified article for network.upload, and without knowing anything about php it worked immediately …

so my advise for everyone, if you want to upload files through Corona, use the PHP file as a webservice … you can download it from this link

https://www.dropbox.com/s/yc3z9a9xgf5kkim/upload.php

follow the setup instruction like security, and folders to be created along with the php file on your hosting server

and in corona use code from this page

https://coronalabs.com/blog/2014/02/25/tutorial-uploading-files-demystified/

but use POST not PUT

and it will work like magic

Finally … i have been searching for months to solve this :-)