Oded 올바른 이미지를 반환하는 처리기를 사용합니다. 예 아래 :
핸들러 클래스 :
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
String Filename = context.Request.QueryString[ "FileName" ];
if (!String.IsNullOrEmpty(Filename))
{
// Read the file and convert it to Byte Array
string filename = context.Request.QueryString[ "FileName" ];
string contenttype = "image/" + Path.GetExtension(Filename.Replace(".", ""));
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32) fs.Length);
br.Close();
fs.Close();
//Write the file to response Stream
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = contenttype;
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Gets whether the handler is reusable
/// </summary>
public bool IsReusable
{
get { return true; }
}
}
나는 그 핸들러 사용하는 일반적인 페이지 방법을 추가 :
/// <summary>
/// Gets the image handler query
/// </summary>
/// <param name="ImagePath">The path to the image</param>
/// <returns>Image Handler Query</returns>
protected string GetImageHandlerQuery(string ImagePath)
{
try
{
if (ImagePath != string.Empty)
{
string Query = String.Format("..\\Handlers\\ImageHandler.ashx?Filename={0}", ImagePath);
return Query;
}
else
{
return "../App_Themes/Dark/Images/NullImage.gif";
}
}
catch (Exception)
{
throw;
}
}
을 그리고 마지막으로 ASPX에서 사용 :
<asp:ImageButton ID="btnThumbnail" runat="server" CommandName="SELECT" src='<%# GetImageHandlerQuery((string)Eval("ImageThumbnail200Path")) %>'
ToolTip='<%#(string) Eval("ToolTip") %>' Style="max-width: 200px; max-height: 200px" />
또는 코드에서 사용하려는 경우 뒤에 :
imgPicture.ImageUrl = this.GetImageHandlerQuery(this.CurrentPiecePicture.ImageOriginalPath);
분명히 페이지 메서드가 필요 없으며 직접 처리기를 호출 할 수 있지만 기본 페이지 클래스를 넣는 것이 유용 할 수 있습니다.