Webserver and large file for download

Hello
I want to put a large file on my SD card (connected to raptor) for download using a webserver based on netmf 4.3. I used codeplex webserver43 and patched responder.cs as it was mentioned in lots of threads…
So, the main problems are:

  1. there is no handling on name of downloading file. How can I set the name of downloading file?

and
*2) as you know there is a limit on making byte array because of memory… So, I have to split my file to some parts and then send them to client. This is my code:


FileStream fileStream = new FileStream(fileAddress, FileMode.Open, FileAccess.Read);
                long TotalPackages = fileStream.Length / PackSize;
                     for (int j = 0; j < TotalPackages; j++)
                    {
                        FileData = new byte[PackSize];
                        fileStream.Read(FileData, 0, PackSize);
                        // first packet
                        if (j == 0)
                        { responder.FileRespond(FileData, false);}
                        else{responder.FileRespond(FileData, true);}
                    }
                    byte[] FileData1 = null;
                    if (fileStream.Length % PackSize != 0)
                    {long TDG = fileStream.Length / PackSize;
                        int length = unchecked((int)(fileStream.Length - (TDG * PackSize)));
                        FileData1 = new byte[length];
                        fileStream.Position = unchecked((int)(TDG * PackSize));
                        fileStream.Read(FileData1, 0, length);
                        responder.FileRespond(FileData1, true);}
responder.closeConnection();
                fileStream.Close();

and I changed the responder class as follow:

 
public void FileRespond(byte[] data, bool firstPacket)
        {
            if (data != null)
            {   webEvent.ContentType = "application/octet-stream";
                webEvent.ResponseData = data;}
            mySendResponse(firstPacket);
         }
         internal void mySendResponse(bool firstPacket)
        {
            try
            {   BinaryResponseTemplate template = this.webEvent.ComputeResponse();
   if (!firstPacket)
                {
                    if (template.Header != null && template.Header.Length > 0)
                    {
                        this.ClientSocket.Send(template.Header, 0, template.Header.Length, SocketFlags.None);
if (template.Content != null && template.Content.Length > 0)
                        {
                         this.ClientSocket.Send(template.Content, 0, template.Content.Length, SocketFlags.None);
                        }
                    }
                }
                else
                {
                    if (template.Content != null && template.Content.Length > 0)
                    {
                        this.ClientSocket.Send(template.Content, 0, template.Content.Length,                                          SocketFlags.None);
                    }

                }
            }
            catch (Exception e)
            {
                Debug.Print("Exception sending web event response (connection was probably terminated) ");
            }
            
        }
  public void closeConnection()
        {
            try
            {
                this.ClientSocket.Close();
            }
            catch { }
        }


but, when second packet is going to be send this error will be appeared:

I know it probably means that the client has closed the connection… So what can I do?
Thanks

Hi,
I’m not sure if this can help. The project shows a WiFi http Server on a FEZ Device and a http Client Windows Forms Application downloading picture files from SD Card. May be that there is a timeout on your client that makes the client close the Connection

https://www.ghielectronics.com/community/codeshare/entry/927