C# 如何将图像发送到浏览器
我正在构建一个简化的web服务器,我能够正确地处理HTML页面的发送 但是当我收到一个图像请求时,我的代码不会给浏览器该图像C# 如何将图像发送到浏览器,c#,asp.net,C#,Asp.net,我正在构建一个简化的web服务器,我能够正确地处理HTML页面的发送 但是当我收到一个图像请求时,我的代码不会给浏览器该图像 FileStream fstream = new FileStream(tempSplitArray[1],FileMode.Open,FileAccess.Read); //The tempSplitArray //recieves the request from the browser byte[] ar = new byte[(long)fstream.Lengt
FileStream fstream = new FileStream(tempSplitArray[1],FileMode.Open,FileAccess.Read);
//The tempSplitArray //recieves the request from the browser
byte[] ar = new byte[(long)fstream.Length];
for (int i = 0; i < ar.Length; i++)
{
ar[i] = (byte)fstream.ReadByte();
}
string byteLine = "Content-Type: image/JPEG\n" + BitConverter.ToString(ar);
sw.WriteLine(byteLine);//This is the network stream writer
sw.Flush();
fstream.Close();
FileStream fstream=newfilestream(tempSplitArray[1],FileMode.Open,FileAccess.Read);
//tempSplitArray//接收来自浏览器的请求
字节[]ar=新字节[(长)fstream.Length];
对于(int i=0;i
请原谅我的无知,如果有任何问题,或者我的问题不够清楚,请告诉我。基本上,您希望您的回答如下所示:
HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Length: *length of image*
Binary Image Data goes here
我假设sw
是一个StreamWriter
,但是您需要写入图像的原始字节
那么:
byte[] ar;
using(FileStream fstream = new FileStream(tempSplitArray[1],FileMode.Open,FileAccess.Read);)
{
//The tempSplitArray //recieves the request from the browser
ar = new byte[(long)fstream.Length];
fstream.read(ar, 0, fstream.Length);
}
sw.WriteLine("Content-Type: image/jpeg");
sw.WriteLine("Content-Length: {0}", ar.Length); //Let's
sw.WriteLine();
sw.BaseStream.Write(ar, 0, ar.Length);
使用诸如查看浏览器和(真实的)web服务器之间的通信并尝试复制这些通信的工具确实很有帮助。您使用的是web表单吗?这是您的处理程序代码吗?不,我正在构建控制台应用程序,通过键入127.0.0.1/index.html从浏览器发送请求,该页面有图像标记,因此浏览器发送请求请求图像,上面的代码应该处理该请求,但它不是:(请执行google stackoverflow:)为什么要将字节转换为字符串?