Here's a quick example how to upload and display an image from a folder. First you need to create a folder that your application has read/write access permissions to it. Assuming that you have a folder named "ImageStorage" within the root of your application that stores the images.
Then you have this HTML markup:
<asp:fileupload id="FileUpload1" runat="server" xmlns:asp="#unknown" />
<asp:button id="Button1" runat="server" text="Upload" onclick="Button1_Click" xmlns:asp="#unknown" />
<br />
<asp:image id="Image1" runat="server" xmlns:asp="#unknown" />
Here's the code associated.
protected void Button1_Click(object sender, EventArgs e) {
StartUpLoad();
}
private void StartUpLoad() {
string imgName = FileUpload1.FileName;
string imgPath = "ImageStorage/" + imgName;
int imgSize = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") {
if (FileUpload1.PostedFile.ContentLength > 10240) {
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
} else {
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = "~/" + imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);
}
}
}