| Following example implemented binary data downloading. using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace cscgi
{
class CsCGI
{
private static string dbgString = "id=1234&pic=123.gif";
private List<string> lstKeys = new List<string>();
private List<string> lstVals = new List<string>();
static void Main(string[] args)
{
CsCGI app = new CsCGI();
ParseParams(app);
string sID = GetParam(app, "id");
string sFile = GetParam(app, "pic");
try
{
FileStream fs = new FileStream(sFile, FileMode.Open);
BinaryReader brFile = new BinaryReader(fs, Encoding.UTF8);
byte[] bBuff = new byte[fs.Length];
brFile.Read(bBuff, 0, (int)fs.Length);
string sBuff = Encoding.ASCII.GetString(bBuff);
// if it's just text file, change binary/plain to text/html
Console.Write("Content-Type: binary/plain ");
Console.Write("Content-Length: " + fs.Length.ToString());
Console.Write(" ");
Console.Write(sBuff);
}
catch (Exception e)
{
Console.Write(e.Message + " ");
}
finally
{
//
}
}
private static void ParseParams(CsCGI app)
{
//string queryString = Environment.GetEnvironmentVariable("QUERY_STRING");
string queryString = dbgString;
string[] sParams = queryString.Split('&');
string[] sParamsSeperated;
for (int i = 0; i < sParams.Length; i++)
{
sParamsSeperated = sParamsIdea.Split('=');
app.lstKeys.Add(sParamsSeperated[0]);
app.lstVals.Add(sParamsSeperated[1]);
}
}
private static string GetParam(CsCGI app, string key)
{
int idx = app.lstKeys.BinarySearch(key);
if (idx >= 0)
return app.lstVals[idx];
return "";
}
}
}
If you are using Apache and wanna test it just put the EXE in the cgi-bin folder under Apache's directory. Also note that the parameters are static just for this example. |