WEBSERVER EXTENSION FOR FEZ COBRA using SD Card help

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

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 ?

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();
 }

Thanks for the time, will give it a try.

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.

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 ?

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


        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);
            }
        }






        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);
            }
        }




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");
            }
        }

@ 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.

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

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 ?

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

Gotcha, thanks.

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.

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";



}


use this:


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

Thanks all, its working now :wink:

ok, got another one.

what if its a BinaryResources file


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

Same thing except Resources.GetBytes