Main Site Documentation

WEBSERVER EXTENSION FOR FEZ COBRA using SD Card help


#1

Wouter Huysentruit, was generous enough to give this example:
http://code.tinyclr.com/project/243/webserver-extension-for-fez-cobra/

I would like to add the ability to get my web files from SD card instead.
Can anyone provide a bit of help for the newbie ?


#2

The webserver handles two type of requests.

  1. You can add files
    To add a single file: call AddFile
    To add a complete directory with subdirs: call AddVirtualDirectory
    The first argument is the virtual path on the webserver: so “/” means the file will be accessible from the root. When you call: AddFile( “/test” , “SDpath\image.jpg” ) it will host the image.jpg on url http:///text

(If you need a hint on how to access the SD card, see another example: http://code.tinyclr.com/project/362/rlp-streaming-audio-driver/ )

  1. You can add events
    F.e. you want to host dynamic data:

 AddEvent("/readtemp", webServer_ReadTemp);

 private static void webServer_Capabilities(object sender, WebServer.EventCallbackEventArgs e)
 {
            e.ResponseContentType = "text/xml";
            e.Response = ReadTempAsXml();
 }


#3

Thanks for the time, will give it a try.


#4

The webserver also has a DefaultFile property which sets the default file of a virtual directory. DefaultFile is set to ‘index.html’ by default.

F.e. when you call


AddVirtualDirectory("/mysite", "path", true);

All files in ‘path’ will be hosted on http:///path/filename.ext
when browsing to http:///path/ the DefaultFile (index.html) will be transmitted.


#5

My brain just poofed smoke out my ears. This is far beyond what i know. Will have to finish the class i am taking and revisit this at a later date :wink:

I do thank you for the time though.

P.S just out of curiosity, can one also add the web files to the project instead of the SD if they wanted to ?


#6

are you on google+ or facebook ? otherwise give me your e-mail so I can add you, I’m willing to get that beast started on your board :slight_smile:

Or join the chat on tinyclr


#7

        public static void Main()
        {
            using (WebServer server = new WebServer())
            {
                webServer.AddVirtualDirectory("/", volume.RootDirectory, true);
                //server.AddEvent("/", new WebServer.EventCallback(sendForm));
                //server.AddEvent("/form", new WebServer.EventCallback(analyseForm));

                // Sleep forever
                Thread.Sleep(Timeout.Infinite);
            }
        }





#8


        public static void Main()
        {

            if (PersistentStorage.DetectSDCard())
            {
                using (PersistentStorage storage = new PersistentStorage("SD"))
                {
                    storage.MountFileSystem();
                    VolumeInfo volume = new VolumeInfo("SD");
                }
            }
            else
            {
                Debug.Print("SD card not detected");
            }


            using (WebServer server = new WebServer())
            {
                server.AddVirtualDirectory("/", volume.RootDirectory, true);
                //server.AddEvent("/", new WebServer.EventCallback(sendForm));
                //server.AddEvent("/form", new WebServer.EventCallback(analyseForm));

                // Sleep forever
                Thread.Sleep(Timeout.Infinite);
            }
        }




#9

public static void Main()
        {
            if (PersistentStorage.DetectSDCard())
            {
                using (PersistentStorage storage = new PersistentStorage("SD"))
                using (WebServer server = new WebServer())
                {
                    storage.MountFileSystem();
                    VolumeInfo volume = new VolumeInfo("SD");

                    server.AddVirtualDirectory("/", volume.RootDirectory, true);

                    Thread.Sleep(Timeout.Infinite);
                    storage.UnmountFileSystem();
                }
            }
            else
            {
                Debug.Print("SD card not detected");
            }
        }


#10

@ Wouter,

Thank you again so much for the chat session. Its working great, and i just cannot thank you enough for taking the time to guide me.


#11

you’re welcome. we’re always happy to help :slight_smile:


#12

Can one get the files from flash instead of from SD card ?
In my project can i add the web site files to the resource folder and have it grab them from there ?


#13

Of course. You use AddEvent like in the example and then read your resource file and return it.


#14

Gotcha, thanks.


#15

The only difference will be that, when hosting files from SD-card, the file is read in smaller blocks, so less memory is used. Now you’ll have to load the whole file in RAM and duplicate it, to pass it as a result.


#16

how exactly am i getting my resource file to work in the call ?

// This is my resource file
Resources.StringResources.index



private static void sendForm(object sender, WebServer.EventCallbackEventArgs e)
{
  
   // IS this what would go here ?
    e.Response = Resources.StringResources.index;

    e.ResponseContentType = "text/html";



}



#17

use this:


e.Response = Resources.GetString(Resources.StringResources.index);
e.ResponseContentType = "text/html";


#18

Thanks all, its working now :wink:


#19

ok, got another one.

what if its a BinaryResources file


e.Response = Resource.BinaryResources.index1;
e.ResponseContentType = "application/x-shockwave-flash";


#20

Same thing except Resources.GetBytes