Loading ...

Image And File Uploading Using ASP.NET | CodeAsp.Net

Image And File Uploading Using ASP.NET

 /5
0 (0votes)

 

I want to share that, How to upload a file or an image through asp.net.

First of you have to take a file upload control for browsing the file and button for uploading it at specified location.

<asp:FileUpload ID="image_upload" runat="server" /> 

<asp:Button ID="btnUpload" runat="server" Text="Upload"  OnClick="btnUpload_Click" />

after that you have write following functions for uploading of specified type of file/image.

For uploading the file

private string uploadimage()
    {

        string filepath = string.Empty;
        string savepath = string.Empty;
        string virtualfolder = string.Empty;
        filepath = Server.MapPath("./") + "image";
        if (image_upload.PostedFile.ContentLength != 0)
        {
            string filename = string.Empty;
            filename = Path.GetFileName(image_upload.PostedFile.FileName);
            string extention = GetExtension(filename);
            if (extention == "jpg" || extention == "gif" || extention == "txt" || extention == "doc")
            {
                savepath = "~/image/" + filename; //foldername
                image_upload.PostedFile.SaveAs(filepath + "/" + filename);
                return savepath;
            }
            else
                return null;
        }
        else
        {
            savepath = "~/image/blankimg.gif";
            return savepath;

        }
    }

 

// Method to check the file type: this Method return the extension of a file which you wish to upload
    private string GetExtension(string FileName)
    {
        string[] split = FileName.Split('.');
        string Extension = split[split.Length - 1];
        return Extension;
    }

 

Hope this will helps..

Comments (no comments yet)

Top Posts