I'm trying to upload a file in ASP.NET C#

Here’s my form…

<form method="post" enctype="multipart/form-data">
   <input type="file" name="filScreenshot" id="filScreenshot" /><br />
   <input type="submit" name="btnSubmit" value="Submit" />

How does my .NET code need to look in order to upload a file? I’ve searched all over, but there seems to be so many ways to do it and none are really working.


Where is the backend and button action?
Are you in Visual Studio? it’s C# or VB.Net?
This code will not work at all.

Just checkout the Microsoft example:

also CodeProject:

C#. I have all the backend code in place except for the code that takes the selected file from filScreenshot and uploads it to my server.

I’m trying baby steps. What can I use to ensure a file was uploaded? Nothing in those links are working for me.

For example…


That gives me this error: ‘System.Web.HttpPostedFile’ does not contain a definition for ‘HasFile’

I found something that works. I can take it from here. Thanks!

if (Request.Files.Count > 0) {
     HttpPostedFile file = Request.Files[0];

Download Visual Web Developer at Microsoft. http://www.asp.net/vwd
You should start coding in a visual environmen, with auto complete abilities it’s more easy to do jobs! you are coding like ASP3 Classic in ASP.Net. but everything has changed, You have fundamental problems in your codes.

<form id="form1" runat="server">
        <h2>Upload File</h2>

        <asp:FileUpload ID="fuUpload" runat="server" />
        <asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click"
            Text="Upload" />

        <p><asp:Label ID="lblInform" runat="server"></asp:Label></p>


hop this helpful

That doesn’t – you don’t even have a key attribute on the form to make it post uploaded files.