• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 924
  • Last Modified:

C sharp - file upload issue

I have a form and it is supposed to upload files to the server.
Attached is the code:

This is the error I get  "{"Object reference not set to an instance of an object."}"
ASP.NET Form
-----------------------------------------------------------------------------------------
    <form id="form1" runat="server">
    <div>
        &nbsp;<table>
            <tr>
                <td colspan="3" style="height: 21px">
                    Upload Files</td>
            </tr>
            <tr>
                <td style="width: 100px">
                    Choose a file to upload</td>
                <td style="width: 100px">
                    <input id="File1" type="file" /></td>
                <td style="width: 100px">
                    <input id="Button1" type="button" value="Upload" runat="server" onserverclick="Button1_ServerClick" /></td>
            </tr>
        </table>
    
    </div>
    </form>
 
C# Code Behind
-----------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page 
{
    protected System.Web.UI.HtmlControls.HtmlInputFile File1;
    protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_ServerClick(object sender, EventArgs e)
    {
        if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))     <--- Error points here
        {
            string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
            string SaveLocation = Server.MapPath("Upload") + "\\" + fn;
            try
            {
                File1.PostedFile.SaveAs(SaveLocation);
                Response.Write("The file has been uploaded.");
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
                //Note: Exception.Message returns a detailed message that describes the current exception. 
                //For security reasons, we do not recommend that you return Exception.Message to end users in 
                //production environments. It would be better to put a generic error message. 
            }
        }
        else
        {
            Response.Write("Please select a file to upload.");
        }
    }
}

Open in new window

0
Isaac
Asked:
Isaac
2 Solutions
 
cluebeckCommented:
http://support.microsoft.com/kb/323246

<INPUT type=file id=File1 name=File1 runat="server" />
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Now I get this error:

Compiler Error Message: CS0102: The type '_Default' already contains a definition for 'File1'
Line 13:     protected System.Web.UI.HtmlControls.HtmlInputFile File1;
0
 
amxCommented:
delete line 13
Line 13:     protected System.Web.UI.HtmlControls.HtmlInputFile File1;
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now