Uploading Image to a Folder and Display the Image after Upload

I wrote a series of blog posts awhile back before that demonstrates the following:

In this post I'm going to demonstrate how to upload image to a specified folder within the root of the web application and  display the image right away in the Image control after uploading. To get started lets go ahead and fire up Visual Studio and create a new Website/Web Application project. After that create a folder under the root application for storing the uploaded images. The folder structure would look something like this below:

Solution   -Application Name   -AppCode   -AppData   -ImageStorage   - //we will save the images in this folder   -Default.aspx   -web.config

Now lets design our WebForm. For the simplicity of this demo, I just set up the form like below:

ASPX:

1: <asp:FileUpload ID="FileUpload1" runat="server" />

2: <asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click"/>

3:

4: <asp:Image ID="Image1" runat="server" />

And here's the code for uploading the image to a folder.

CODE BEHIND:

1: protected void Button1_Click(object sender, EventArgs e) {

2: StartUpLoad;

3: }

4:  

5: private void StartUpLoad {

6: //get the file name of the posted image

7: string imgName = FileUpload1.FileName;

8: //sets the image path

9: string imgPath = "ImageStorage/" + imgName;

10: //get the size in bytes that

11:  

12: int imgSize = FileUpload1.PostedFile.ContentLength;

13:

14: //validates the posted file before saving

15: if (FileUpload1.PostedFile!= null && FileUpload1.PostedFile.FileName!= "") {

16: // 10240 KB means 10MB, You can change the value based on your requirement

17: if (FileUpload1.PostedFile.ContentLength > 10240) {

18: Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);

19: }

20: else {

21: //then save it to the Folder

22: FileUpload1.SaveAs(Server.MapPath(imgPath));

23: Image1.ImageUrl = "~/" + imgPath;

24: Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);

25: }

26:  

27: }

28: }

As you can see the code above is very straight forward and self explanatory. Here are the sample screen shots of the output:

On initial load:

!(/images/dotnetvinz/uploading-image-to-a-folder-and-display-the-image-after/5bdadf4c-imageupload1.jpg)

Browsing an image:

!(/images/dotnetvinz/uploading-image-to-a-folder-and-display-the-image-after/91f97965-imageupload2.jpg)

After uploading:

!(/images/dotnetvinz/uploading-image-to-a-folder-and-display-the-image-after/82d77e85-imageupload3.jpg)

That's it! I hope someone find this post useful!

This article is part of the GWB Archives. Original Author: Vincent Maverick Durano

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.