我在更改图片字段的imageUrl时遇到问题。我想通过单击名为“upload”的按钮来显示上传的图像。 这是我的服务器端代码:
if (fileUpload.HasFile)
{ Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/uploads/logo"));
string targetFolder = HttpContext.Current.Server.MapPath("~/uploads/logo");
string targetPath = Path.Combine(targetFolder, "photo.png");
fileUpload.SaveAs(targetPath);
employeeImage.ImageUrl = targetPath;
}
答案 0 :(得分:0)
我通过使用webhandler(.ashx)来解决这个问题。
public class ImageHandler:IHttpHandler,System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest (HttpContext context) {
//Checking whether the imagebytes variable have anything else not doin anything
if ((context.Session["ImageBytes"]) != null)
{
byte[] image = (byte[])(context.Session["ImageBytes"]);
context.Response.ContentType = "image/JPEG";
context.Response.BinaryWrite(image);
}
}
public bool IsReusable {
get {
return false;
}
}
}
和服务器端代码是:
protected void uploadButton_Click(object sender, EventArgs e)
{
Session["ImageBytes"] = fileUpload.FileBytes;
employeeImage.ImageUrl = "~/ImageHandler.ashx";
}