我想尝试使用Web API进行休息调用,但我希望响应是存储在数据库中的实际二进制映像,而不是JSON base64编码的字符串.有人对此有一些指示吗?
更新 - 这是我最终实现的:
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new MemoryStream(profile.Avatar));
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "avatar.png";
return result;
Run Code Online (Sandbox Code Playgroud) 我的aspx页面中有以下图像
<td>
<asp:Image ID="LargeImage" runat="server" Height="100" Width="100" />"
</td>
Run Code Online (Sandbox Code Playgroud)
在我的aspx.cs中,为此图像指定了一个图像
protected void uploadimage_Click(object sender, System.EventArgs e)
{
ImageUtils.uploadImage(Titletxt.Text, FileUpload.FileContent);
LargeImage.ImageUrl = "~/AvatarImageFetch.ashx?memberid=" + memberid.ToString();
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,图像不会显示.这是我的ashx
public void ProcessRequest(HttpContext context)
{
SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["FMMImages"].ConnectionString);
myConnection.Open();
string sql = "select largeimage from images_temp where id=@memberid";
SqlCommand cmd = new SqlCommand(sql, myConnection);
int param;
int.TryParse(context.Request.QueryString["memberid"], out param);
cmd.Parameters.Add("@memberid", SqlDbType.Int).Value = param;
//cmd.Parameters.Add("@GuID", SqlDbType.UniqueIdentifier).Value = context.Request.QueryString["UID"].ToString();
cmd.CommandType = System.Data.CommandType.Text;
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["largeimage"]);
dReader.Close();
myConnection.Close();
}
Run Code Online (Sandbox Code Playgroud)
另外,我在ashx处理程序中有一个断点.看起来处理程序没有触发.