简体   繁体   中英

how to read an image file in asp.net c# using asp:fileupload?

I used the code below for upload an image. Please let me know that why this code does not work at all. I am also using updatePanal and multiview control for tab controlling.

<asp:FileUpload ID="fuPhoto" runat="server"/>
<div style="margin-top:20px;text-align:center;">
    <asp:Button ID="btnAddMemberInfo" runat="server" Text="Add" Width="100px" onclick="btnAddMemberInfo_Click" />                                
</div>

public byte[] GetPhtoStream()
            {
                byte[] bufferPhoto = new byte[fuPhoto.PostedFile.ContentLength];
                Stream photoStream = fuPhoto.PostedFile.InputStream;
                photoStream.Read(bufferPhoto, 0, fuPhoto.PostedFile.ContentLength);
                return bufferPhoto;
            }
protected void btnAddMemberInfo_Click(object sender, EventArgs e)
        {
  Photo = GetPhtoStream(); //Photo represent for the database field which datatype is image
}

FileUpload doesn't work in UpdatePanel with ajax postbacks. To get it work You have to register btnMemberInfo as PostBackTrigger for full postback. Then it'll work.

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