简体   繁体   中英

how to preview uploaded image before saving it in database in image control in asp.net

I am allowing the user to upload the image. as soon as user uploads the image the respective image should get displayed in image control(similar to the preview of image). this should be done without clicking any button

<td align="right" valign="top" class="heading">Photo: </td>
<td>
    <asp:Image ID="Image1" runat="server" /><br />
    <asp:FileUpload ID="FileUpload1" runat="server"/>
</td>

As you have been asking for complete code and i have no idea what it looks like here comes a piece of code with lots of assumptions...

public void UploadImage(object sender, EventArgs e)
{
    //here you upload your image to a predefined Path and save it...
    File image = FileUpload1.getFile();// don't know how to handle it, haven't used it b4
    image.Save(path); //path is your savepath, even if its temporary
    Image1.ImageUrl = path.ToUrl();
    //alternatively do the following:
    Image1.ImageUrl = FileInfo(image).getFullPath();

    //in general you need to update controls before they show what they got so:
    Image1.Update(); / Image1.Refresh();
}

I recently had same requirement as you ... I used AjaxFileUploader instead of normal FileUpload control .. and a Image Handler .. This helped me in achiecving it..

Preview Image before uploading

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