public static string GetAvatar()
{
logger.Info("Start--GetAvatar");//aatif
// string headerText = "Bearer " + token;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.mxit.com/user/public/avatar/id");
//request.Method = method;
//request.Headers.Add(HttpRequestHeader.Authorization, headerText);
//if (contentType != null)
//{
// request.ContentType = contentType;
//}
string method = "GET";
if (method == "GET")
{
try
{
WebResponse response = request.GetResponse(); // Byte Stream
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
string JsonGET = responseFromServer.ToString();
// Avatar res = JsonConvert.DeserializeObject<Avatar>(JsonGET);
reader.Close();
dataStream.Close();
response.Close();
logger.Info("End--GetAvatar");//aatif
return JsonGET;
}
catch (Exception ex)
{
logger.Error("CacheData.GetAvatar():" + ex.StackTrace); /// Aatif
}
}
logger.Info("End--GetAvatar"); ///aatif
return null;
}
string avatar = MURLEngine.GetAvatar();
在前端显示图片:
<span id="ProfileImage">
<img src="data:image/png;base64,@Model.avatarimage" />
</span>
如何在前端显示字节流图像?我现在无法这样做。
答案 0 :(得分:4)
你关闭了。转换为base64。
(其他答案并不直接与您对WebRequest的需求有关。)
public static string GetAvatar()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.mxit.com/user/public/avatar/vini.katyal");
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
MemoryStream ms = new MemoryStream();
responseStream.CopyTo(ms);
byte[] buffer = ms.ToArray();
string result = Convert.ToBase64String(buffer);
response.Close();
responseStream.Close();
return result;
}
答案 1 :(得分:0)
我建议您在控制器中创建一个GET方法,该方法接受您的行或其他内容的ID。基于此,只需获取字节数组并将其发送回作为contentresult。这是一个例子:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Image(int id)
{
byte[] byteArray = _imageRepository.GetImage(id);
return new FileContentResult(byteArray, "image/jpeg");
}
在视图中,您可以通过以下方式调用此控制器方法:
<img src="Image/1234" alt="Image 1234"/>
答案 2 :(得分:0)
尝试使用Convert类方法。您可以使用Convert.ToBase64String
方法将字节数组转换为图像的base64字符串表示。
来自MSDN的示例:
try {
inFile = new System.IO.FileStream(inputFileName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0,
(int)inFile.Length);
inFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message);
return;
}
// Convert the binary input into Base64 UUEncoded output.
string base64String;
try {
base64String =
System.Convert.ToBase64String(binaryData,
0,
binaryData.Length);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Binary data array is null.");
return;
}
以下是我found的另一个例子。
答案 3 :(得分:0)
我解决了这个问题:
WebResponse response = request.GetResponse();
byte[] b = null;
using (Stream stream = response.GetResponseStream())
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
count = stream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (stream.CanRead && count > 0);
b = ms.ToArray();
}
return Convert.ToBase64String(b);
我首先将响应转换为Stream然后转换为Byte数组,然后转换为base64字符串