C# .NET - file upload - Asked By deepu p on 28-Jun-12 08:06 AM

bharti odedra replied to deepu p on 28-Jun-12 08:12 AM

This is very basic level small user control, in which we can discuss how to create web user control.

To create user controls follow the steps:

  1. Right click on project
  2. Click on Add
  3. Select New Item
  4. Select Web User Control
  5. Specify some Name

I have given name to this file as "uploader.ascx" and kept this file under "userControl" for simplicity purpose.

On this page I am having following controls:

  1. <INPUT id="fileUpload" type="file" Runat="server" NAME="fileUpload">

  2. <asp:label id="lblMessage" runat="server" Width="416px" Font-Size="10" Font-Name="verdana"></asp:label> 

  3. <asp:button id="btnSave" runat="server" Text="Upload.."></asp:button>

At the code behind of above file I am having following function

public string uploadFile(string fileName,string folderName)

{

      if(fileName=="")

      {

            return "Invalid filename supplied";

      }

      if(fileUpload.PostedFile.ContentLength==0)

      {

            return "Invalid file content";

      }

            fileName = System.IO.Path.GetFileName(fileName);

      if(folderName=="")

            {

            return "Path not found";

            }  

            try

            {

            if (fileUpload.PostedFile.ContentLength<=2048000)

                  {  

     fileUpload.PostedFile.SaveAs(Server.MapPath(folderName)+"\\"+fileName);  

                       return "File uploaded successfully";  

                 }

                 else

            {

                  return "Unable to upload,file exceeds maximum limit";

                 }

      }

            catch(UnauthorizedAccessException ex)

            {

            return ex.Message + "Permission to upload file denied";

            }  

}

Jitendra Faye replied to deepu p on 29-Jun-12 12:54 AM
For this use FileUpload Control.

Use this code-

protected void UploadButton_Click(object sender, EventArgs e)

{

    if(FileUploadControl.HasFile)

    {

    try

    {

      string filename = Path.GetFileName(FileUploadControl.FileName);

      FileUploadControl.SaveAs(Server.MapPath("~/") + filename);

      StatusLabel.Text = "Upload status: File uploaded!";

    }

    catch(Exception ex)

    {

      StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;

    }

    }

}

Lalit M replied to deepu p on 29-Jun-12 07:25 AM
Use this code
------------
FileUploadControl-ID of your FileUpload Control

btnupload-ID of your Submit/Upload Button Control

protected void btnupload_Click(object sender, EventArgs e)
    {
      if (FileUploadControl.HasFile)
      {
        try
        {
          if (FileUploadControl.PostedFile.ContentType == "image/jpeg")
          {
            if (FileUploadControl.PostedFile.ContentLength < 102400)
            {
              string filename = Path.GetFileName(FileUploadControl.FileName);
              FileUploadControl.SaveAs(Server.MapPath("../images/") + filename);
              StatusLabel.Text = "Status: File uploaded!";
              getfilename();
            }
            else
              StatusLabel.Text = "Status: The file has to be less than 100 kb!";
          }
          else
            StatusLabel.Text = "Status: Only JPEG files are accepted!";
        }
        catch (Exception ex)
        {
          StatusLabel.Text = "Status: The file could not be uploaded.Error: " + ex.Message;
        }
      }

    }

for more code sample
-------------------------
http://shawpnendu.blogspot.in/2009/05/file-upload-with-aspnet-c.html