Hi I'm trying to make a file upload function for pictures.
So can anyone please help me to make a simple file upload function in c#?

I've tried to make one, but it won't seem to work, so I've decided to start over, just with a simple file upload funktion.

Hope someone can help?

HI,
Add fileupload control in your page. Then

if (null != file.PostedFile) 
  {
    try 
    {
if (file.ContentType.IndexOf("excel") >= 0)
file.SaveAs(Server.MapPath(@"\Upload\Excel\" + filename));
else if (file.ContentType.IndexOf("word") >= 0)
file.SaveAs(Server.MapPath(@"\Upload\Word\" + filename));
else
file.SaveAs(Server.MapPath(@"\Upload\Other\" + filename));    
}
    catch (Exception e) 
    {

    }
  }

Now I tryed that, but it didn't work.

Under: filename a red line appears, and if I hold the mouse on that it writes: The name 'filename' does not exist in the current context.

and under ContentType a red line also appears witch says:

'System.web.ui.webcontrols.Fileupload' does not contain a definition for 'contenttype' and no extension method 'contenttype' accepting a first argument of type
'System.web.ui.webcontrols.Fileupload' could be found (are you missing a using directive or an assembly refference?)

Do you know how to fix it? please?

my whole code on my cs page looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class _Default : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

        if (null != file.PostedFile)
        {

            try
            {

                if (file.ContentType.IndexOf("excel") >= 0)

                    file.SaveAs(Server.MapPath(@"\Upload\Excel\" + filename));

                else if (file.ContentType.IndexOf("word") >= 0)

                    file.SaveAs(Server.MapPath(@"\Upload\Word\" + filename));

                else

                    file.SaveAs(Server.MapPath(@"\Upload\Other\" + filename));

            }

            catch (Exception e)
            {

            }
        }
    }
}

Replace filename by

file.PostedFile.FileName

ALSO REPLACE file.ContentType BY

file.PostedFile.ContentType

Also makesure that you have cretaed a folder named Upload in root & also create another 3 folders named Excel,Word,Other as subfolder.

Now I've tried to test my code, but it doesn't seem to save the files in the folders iv'e created.
But it doesn't write any errors, it just doesn't save the files.

all my code now looks like this on the cs page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class _Default : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

        
    }
    

    protected void upload_Click(object sender, EventArgs e)
    {
                if (null != file.PostedFile)
        {

            try
            {

                if (file.PostedFile.ContentType.IndexOf("excel") >= 0)

                    file.SaveAs(Server.MapPath(@"\Upload\Excel\" + file.PostedFile.FileName));

                else if (file.PostedFile.ContentType.IndexOf("word") >= 0)

                    file.SaveAs(Server.MapPath(@"\Upload\Word\" + file.PostedFile.FileName));

                else

                    file.SaveAs(Server.MapPath(@"\Upload\Other\" + file.PostedFile.FileName));

            }

            catch (Exception)
            {

            }
    }
}
}

and on the aspx page:

<asp:FileUpload ID="file" runat="server" /><br />
        <asp:Button ID="upload" runat="server" Text="upload" onclick="upload_Click"/>

do you know what the problem can be?

Thanks.

Yes the problem is iis user cant write to the upload folder. Means its permission isue. Right click on upload folder which you cretated in your root folder then click properties then click on security tab then add then Write everyone click on checkname & finaly ok.

If problem doesnot resolved yet then print the exception from catch block & post the error into this thread.

If problem resolved then remove everyone permission from upload folder & give the permission to iis default user.

This article has been dead for over six months. Start a new discussion instead.