Author Topic: Upload File  (Read 4195 times)

korn3l

  • Guest
Upload File
« on: April 26, 2012, 12:54:48 pm »
Hi!

Has someone an Idea how I could Upload an Image from my Device?


Thanks,
Kornel

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: Upload File
« Reply #1 on: April 27, 2012, 08:50:46 am »
Hi, Kornel,

It depends on what you mean by "upload an image".

If you simply want to get an image saved to the Documents folder in your app, then you can transfer it to your computer from iTunes.  DragonFireSDK test builds enable the "iTunes File Sharing" feature (the same can be done for your App Store builds if needed) and you can see your Documents folder while the device is connected to iTunes from the Apps tab on the device page.  Scroll down to the bottom of the page and you'll see "Apps that allow file sharing" or something similar.

If you want to upload a file over wifi or cellular networks, you can use the File functions to load the image into a char buffer and you can then send the char representation of the file using NetSend() on the query string.

Hope this helps!
Tim Sommers
DragonFireSDK Team

k

  • Guest
Re: Upload File
« Reply #2 on: June 04, 2012, 11:51:53 am »
Hi tim!

Could you please clarify how to load image into a char buffer, and send the char representation in URL using NetSend() ?

I'm stuck at this point and would really appreciate some help.

Thanks

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: Upload File
« Reply #3 on: June 04, 2012, 02:38:28 pm »
Off the top of my head:

Code: [Select]
#include "DragonFireSDK.h"

char MyFileBuffer[20000]; // Max size for our image is ~20k.
char MyURL[32000]; // Max URL size=32000 bytes.
int MyFile;

int OnResponse(int event, char *response, int len)
{
// Process response...
return(1);
}

void AppMain()
{
// Send a file using NetSend():
MyFile=FileOpen("MyImage.png");
FileRead(MyFile,MyFileBuffer,20000);
FileClose(MyFile);
MyFileBuffer[20000]=0; // Make sure the buffer is NULL terminated.
sprintf("http://www.myserver.com/imagefile=%s",MyFileBuffer);
NetSend(MyURL,OnResponse);
}

void OnTimer()
{

}

void AppExit()
{

}

This will load an image file from Documents and send it to the specified URL.  You may need to change some of the values to suit your needs, such as the size of the image file.  The max size of a URL is 32000 bytes.
Tim Sommers
DragonFireSDK Team

k

  • Guest
Re: Upload File
« Reply #4 on: June 05, 2012, 01:30:34 am »
Thanks tim,

I finally got a response now, I'm displaying the response from the webserver with TextSetText, and the variable contains a lot of strange characters.
Now my only issue is how to receive the data at server side, and save the file to disk in my asp.net application.

k

  • Guest
Re: Upload File
« Reply #5 on: June 05, 2012, 03:34:37 am »
Well, now I'm not even sure the querystring data sent to my server is correct. The Windows Server Event log show this request URL:

Code: [Select]
http://<server-ip>/Default.aspx?imagefile=%FFPNG%0D%0A%1A%0A
My app.cpp looks like this:

Code: [Select]
//====================================================
// App.cpp
//====================================================
#include "DragonFireSDK.h"

char MyFileBuffer[20000]; // Max size for our image is ~20k.
char MyURL[32000]; // Max URL size=32000 bytes.
int MyFile;
int MyFont;

int MyText;

int OnResponse(int event, char *response, int len)
{
  TextSetText(MyText, response);
  return event;
}

void AppMain()
{
// Application initialization code goes here.  Create the items / objects / etc.
// that your app will need while it is running.
// Send a file using NetSend():
MyFile=FileOpen("Submit2.png"); //this file is located at /Debug/Documents/Submit2.png
FileRead(MyFile,MyFileBuffer,20000);
FileClose(MyFile);
MyFileBuffer[20000]=0; // Make sure the buffer is NULL terminated.
sprintf(MyURL,"http://<server-ip>/default.aspx?imagefile=%s",MyFileBuffer);
NetSend(MyURL,OnResponse);
MyFont = FontAdd("Helvetica", "Regular", 12, 0x00FF00);
MyText = TextAdd(0, 0, "Response", MyFont);
}

void AppExit()
{
// Application exit code goes here.  Perform any clean up your app might
// need to do in the event of interruption by a phone call or the user
// pressing the Home button, or some other event that would cause your
// application to terminate.
}

void OnTimer()
{
    // Main loop code goes here.  OnTimer() is called 30 times per second.
}

I guess my first step in this process is to make sure the request sent to my server is 100% correct. Hmm.

k

  • Guest
Re: Upload File
« Reply #6 on: June 05, 2012, 03:58:45 am »
Hm, OK. But if I change my PNG-file in app.cpp, it shows exactly the same data: %FFPNG%0D%0A%1A%0A
How can this querystring variable (%FFPNG%0D%0A%1A%0A) really contain my whole image file? I guess I'm left behind in this topic... :)

I want my server to receive the image file from querystring and save it do disk.

k

  • Guest
Re: Upload File
« Reply #7 on: June 05, 2012, 04:09:27 am »
For experimental purpose, I converted another PNG-file from my ASP.NET-application to bytes, and the resulting bytes-string was (about 93 000 characters long):
Code: [Select]
iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAD//0lEQVR42uy9W4xt2XUdttZ+nEe97rO72S+RokWKIpvdkk0pluxIjGiSchwDSfQwECQfQZAA+Q0CJD9BEAT5SPKRDwNBEhhJADuOkx/TsoUIli3YCux8WJZNsUmRoinJlkSym32776.......... AAAASUVORK5CYII=
With this string as request to my server application, I manage to save the image file to disk...

k

  • Guest
Re: Upload File
« Reply #8 on: June 05, 2012, 04:10:59 am »
Just wrote  fast to show you how it used:


Code: [Select]


I'm using my mac right now and my windows based laptop is back home, i'm going take a look and get back to  you, showing how to save the image



I appreciate it, thanks!
« Last Edit: June 05, 2012, 04:12:44 am by k »

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: Upload File
« Reply #9 on: June 06, 2012, 11:43:48 am »
Hello,

Sorry for the long time, i made a function to upload a image that is encoded, but NetSend dont have any timeout option, so the server sometimes keep loading and waiting, they should allowed use a timeout option to say if the server dont response in 60 second' then close the connection. Of Course that can be done with some if statement etc.. but its not going be the same. They should make it. I'm sure they are using socket, why not make a socket timeout option?  i have to find a trick or way to do that.... btw  threads that works on mac is not same on windows, tried them both.... i'm waiting for this update, they should have included... as al developer need it!

This sounds like you need to code your server to respond with some kind of error if it fails to do whatever it needs to do.  Also, keep in mind, as documented, the max URL length is 32000 bytes for NetSend().
Tim Sommers
DragonFireSDK Team