Hello,
I’m trying to send an image from my SD-card to the browser with a webservice, but my keeps saying that the image is corrupt or truncated.
This is the code I am using
string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
string pathName = rootDirectory + @ "\system_Webserver\quotes.gif";
byte[] buf = new byte[4096];
FileStream fileToServe = new FileStream(pathName, FileMode.Open, FileAccess.Read);
long fileLength = fileToServe.Length;
// Once we know the file length, set the content length.
response.ContentLength64 = fileLength;
// Send HTTP headers. Content lenght is ser
Debug.Print("File length " + fileLength);
// Now loops sending all the data.
for (long bytesSent = 0; bytesSent < fileLength; )
{
// Determines amount of data left.
long bytesToRead = fileLength - bytesSent;
bytesToRead = bytesToRead < 4096 ? bytesToRead : 4096;
// Reads the data.
fileToServe.Read(buf, 0, (int)bytesToRead);
// Writes data to browser
//response.OutputStream.Write(buf, 0, (int)bytesToRead);
System.Threading.Thread.Sleep(10);
// Updates bytes read.
bytesSent += bytesToRead;
// Debug.GC(true);
}
fileToServe.Close();
response.ContentType = "image/gif";
messageBytes = buf;
response.StatusCode = (int)HttpStatusCode.OK;
response.ContentLength64 = messageBytes.Length;
using (Stream output = response.OutputStream)
output.Write(messageBytes, 0, messageBytes.Length);
response.Close();
Can anyone help me? I already looked at all the available info about webservices on tinyCLR
Jay_Jay
2
how about this :
static void SendFileOverHTTP(HttpListenerResponse response, string strFilePath )
{
if (strFilePath.IndexOf(".cs") != -1 ||
strFilePath.IndexOf(".txt") != -1 ||
strFilePath.IndexOf(".csproj") != -1
)
{
response.ContentType = "text/plain";
}
if (strFilePath.IndexOf(".jpg") != -1 ||
strFilePath.IndexOf(".bmp") != -1 ||
strFilePath.IndexOf(".jpeg") != -1
)
{
response.ContentType = "image";
}
if (strFilePath.IndexOf(".htm") != -1 ||
strFilePath.IndexOf(".html") != -1
)
{
response.ContentType = "text/html";
}
if (strFilePath.IndexOf(".mp3") != -1)
{
response.ContentType = "audio/mpeg";
}
FileStream fileToServe = null;
try
{
fileToServe = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
long fileLength = fileToServe.Length;
// Once we know the file length, set the content length.
response.ContentLength64 = fileLength;
// Send HTTP headers. Content lenght is ser
Debug.Print("File length " + fileLength);
// Now loops sending all the data.
byte[] buf = new byte[4096];
for (long bytesSent = 0; bytesSent < fileLength; )
{
// Determines amount of data left.
long bytesToRead = fileLength - bytesSent;
bytesToRead = bytesToRead < 4096 ? bytesToRead : 4096;
// Reads the data.
fileToServe.Read(buf, 0, (int)bytesToRead);
// Writes data to browser
response.OutputStream.Write(buf, 0, (int)bytesToRead);
System.Threading.Thread.Sleep(100);
// Updates bytes read.
bytesSent += bytesToRead;
}
fileToServe.Close();
}
catch (Exception e)
{
if (fileToServe != null)
{
fileToServe.Close();
}
throw e;
}
}
was taking directly from the sample folder that is installed on your system
C:\Documents and Settings\[b]YourLoginName[/b]\My Documents\Microsoft .NET Micro Framework 4.1\Samples\HttpServer\HttpServer
and you would call it like this on your event:
static private string GetPathFromURL(string URL)
{
char[] szFilePath = URL.ToCharArray();
for( int i = 0, length = szFilePath.Length; i < length; i++ )
{
if (szFilePath[i] == '/')
{
szFilePath[i] = '\\';
}
}
// Return updated string with \SD in front
return new string( szFilePath );
}
private static void ProcessClientGetRequest( HttpListenerContext context )
{
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
string strFilePath = GetPathFromURL(request.RawUrl); //your path to your image.
response.StatusCode = (int)HttpStatusCode.OK;
// Find if file is present. If file exists - sends its context
if (File.Exists(strFilePath))
{
SendFileOverHTTP(response, strFilePath);
}
}
Enjoy!
I’ve tried it before, I don’t get it to work.
No errors are thrown, but my browser keeps telling me that the image couldn’t be shown because it contains errors
Jay_Jay
4
in that case,
i would try a small image…just to make sure it works.
use MSPAINT to create it.
Ohh, but it’s small, 351 bytes with a resolution of 15px on 13px
Jay_Jay
6
LOL maybe you should try something a little bigger then, a few KB …