上传图像和保存文件流在视图状态
本文关键字:视图状态 保存文件 图像 | 更新日期: 2023-09-27 18:06:40
protected void upload_Click(object sender, EventArgs e)
{
if (Upload.Value !="")
{
System.IO.Stream fs = Upload.PostedFile.InputStream;
img_uploadStream = Upload.PostedFile.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] bytes = CreateThumbnail(br.ReadBytes((Int32)fs.Length),150);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
imageField.Src = String.Format("data:{0};base64,{1}", "image/jpeg", base64String);
}
}
上传图片后,upload . postdfile . inputstream设置为空值。我想保存这个输入流到asp.net viewstate以重用
您可以使用以下代码
public Byte[] YourImage
{
get
{
if(ViewState["Key"] != null)
{
return (Byte[]) ViewState["Key"];
}
return null;
}
set
{
ViewState["Key"] = value;
}
}
Set ViewState inside upload_Click:
ViewState["ImageStream"]=Upload.PostedFile.InputStream;
然后你可以在页面内任何你想使用的地方获得你的视图状态:
System.IO.Stream fstream=(System.IO.Stream)ViewState["ImageStream"];