Send a file from a web page

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Send a file from a web page

Jump to solution
2,502 Views
adyr
Contributor V

Hi,

I am developing a board with a K64 processor using KSDK 1.2 and KDS 3.0 and compiling with IAR workbench. I have implemented an HTTP server on the board and it is providing web pages OK and I have even managed to make use of cgi and shtml to set and show dynamic content.

What I need to do next is find a way to send files to the board via a web page. Can anyone recommend the best way to do this, e.g. TFTP, FTP or other methods on the board and point me in the direction of some help to code a web page that will send the files, probably via a button on the page that pops up a file browser?

My board has and SD card on it so I envisage using that as the temporary storage location for the downloaded file.

Any suggestions would be great.

Best regards,

Adrian.

Labels (1)
0 Kudos
1 Solution
2,132 Views
DavidS
NXP Employee
NXP Employee

Hi Adrian,

I did something like this years ago (2008) so it is not fresh in my mind.

Attached is the HTML Javascript code/file for uploading a file from web page using HTML POST method.

The comments have URL from where I initially got the code.

Hope it helps.

Regards,

David

View solution in original post

6 Replies
2,132 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi Adrian:

The shell example has ftp capability, which is in rtcs folder in the Freescale MQX 4.2 release

C:\Freescale\Freescale_MQX_4_2\rtcs\examples\shell

You can FTP files to the SD card.

Regards

Daniel

0 Kudos
2,132 Views
adyr
Contributor V

Hi Daniel,

Thanks for getting back so quick. I have looked at the example and thankfully MQX makes the implementation of the servers very easy.

The problem I have is creating the web page for the boards web server to hand out that will allow the user to upload a file. I was hoping someone might have some guidance as to the easiest protocol to use from the web page point of view and could point me to some html / javascript examples.

Basically I'm trying to provide the same feature as most routers have of uploading new firmware, web site, FPGA code, etc to the board from it's web interface.

I appreciate this forum is about the hardware / MQX so my quest for knowledge is a little off track here but it is related. Searching the web mainly brings up PHP examples but if I am not mistaken that would not work in this case as MQX has no PHP engine?

If anyone has done such a thing and could pass on some advice I would be very grateful.

Best regards,

Adrian.

0 Kudos
2,133 Views
DavidS
NXP Employee
NXP Employee

Hi Adrian,

I did something like this years ago (2008) so it is not fresh in my mind.

Attached is the HTML Javascript code/file for uploading a file from web page using HTML POST method.

The comments have URL from where I initially got the code.

Hope it helps.

Regards,

David

2,132 Views
adyr
Contributor V

Hi David,

It took me a while to get back to this but it looks like it will do what I want so thank you very much for your help.

So if it helps anyone else, all I had to do was change the form action to point to a cgi function and then I can read the data in that cgi callback. I still have to work out the exact format of the message as it incorporates the filename and content type at the beginning of the actual file data but that should be straight forward.

So in it's simplest form with no checking for the file type etc the html looks like:

<form method=post name=upform action="upload.cgi" enctype="multipart/form-data">

  <input type=file name=uploadfile>

  <p><input id="Submit" type="submit" value="Submit" /></p>

</form>

The CGI code so far is:

static _mqx_int cgi_upload(HTTPSRV_CGI_REQ_STRUCT* param)
{
    HTTPSRV_CGI_RES_STRUCT response = {0};
    response.ses_handle = param->ses_handle;

    if (param->request_method == HTTPSRV_REQ_POST)
    {
       char buffer[ 200 ];
       uint32_t bufSize = 200;
       if (bufSize > param->content_length)
       {
          bufSize = param->content_length;
       }

       //Get the POST data into our buffer
       uint32_t length = param->content_length;
       uint32_t written = 0;
       uint32_t read = HTTPSRV_cgi_read( param->ses_handle, buffer, bufSize );
       if (read > 0)
       {
          int fd = -1;
          if ((fd = open( "a:\\FTP\\webup.txt", O_WRONLY | O_CREAT | O_TRUNC )) > 0)
          {
             while ((read > 0) && (length > 0))
             {
                write( fd, buffer, read );
                length -= read;
                written += read;
                if (length > 0)
                {
                    read = HTTPSRV_cgi_read( param->ses_handle, buffer, (length > bufSize ? bufSize : length) );
                }
             }
   
             close( fd );
         }
      }

      sprintf( buffer, "Saved %li bytes", written );
      return post_OK_response( &response, buffer );
   }

   return (0);
}

The data saved in the file on the SD card (a:) was:

-----------------------------7e03363290c10
Content-Disposition: form-data; name="uploadfile"; filename="C:\Temp\test.txt"
Content-Type: text/plain

"File Content Here"

-----------------------------7e03363290c10--

So now I just have to work out how to parse the header and footer to save just the file content to the specified file, tidy the code up and add some error checking to the form.

Easy when you know how :-)

Adrian.


0 Kudos
2,046 Views
Shivam343
Contributor I

Hi Adrian,

The code works well for uploading file from PC to embedded board. Do you know how we can do reverse i.e. downloading file from embedded Board to PC.

0 Kudos
2,134 Views
adyr
Contributor V

Hi David,

Thanks for the information. I will try to figure out how it works to see if it will do what I need.

Best regards,
Adrian.

0 Kudos