简体   繁体   中英

upload image and save file stream in View State

    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);
    }
}

After upload the image , Upload.PostedFile.InputStream has set to null value . I want to save this Input Stream to asp.net View State to reuse .

You can use this code

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;

Then you can get your viewstate anywhere within the page where you want to use:

System.IO.Stream fstream=(System.IO.Stream)ViewState["ImageStream"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM