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" />
</form>

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.

Thanks!

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:
http://www.codeproject.com/Articles/1757/File-Upload-with-ASP-NET

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…

Request.Files["filScreenshot"].HasFile

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];
     file.SaveAs(Server.MapPath("images/myFile.jpg"));
}

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">
    <div>
        <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>
    </div>

    </form>

hop this helpful

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