-1

I have been pounding my head on this issue for several days and could use some help, maybe a fresh set of eyes would help.

I have Windows Server 2012, IIS 8.0 and ASP.NET 4.5. I am new to both IIS and ASP.NET so please be patient with me. The website I am trying to build allows the user to upload files which will first be checked to make sure they are valid and then will be placed on a folder on the web server.

I tried having the Javascript validate the inputs first before submitting the form to the server. However, nothing is uploaded so I decided to do one step at a time and just do a simple upload (without Javascript validation, for now).

Here is currently how the files stand:

upload_page.aspx

<html>
...
<script language="Javascript">
    function validate()
    {
        var filter = <allowed file extensions>;
        var file1 = document.getElementById("uploadfile1").value;
        //do the checks

        if(filter.test(file1))
        {
            returnval = true;
        }
        else
        {
            returnval = false;
        }

        return returnval;
    }
</script>
...
<body>
    <form method="post" runat="server" name="upload" enctype="multipart/form-data">
        <asp:FileUpload ID="uploadfile1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" onClientClick="btnUpload_Click" />
        <asp:Button ID="btnReset" runat="server" Text="Reset" />
    </form>
</body>
</html>

upload_page.aspx.cs

protected void btnUpload_Click(object sender, EventArgs e)
{
    if(this.uploadfile1.HasFile)
    {
        this.uploadfile1.SaveAs("C:\\inetpub\\wwwroot\\uploaded_files\\" + this.uploadfile1.FileName);
    }
}

If anyone can tell me what I'm doing wrong it would be greatly appreciated! Thanks.

3
  • Where's the action for the form? Commented Nov 10, 2014 at 17:29
  • Actually, you have nothing in your form to submit it. Commented Nov 10, 2014 at 17:30
  • Well, I am confused because I had taken it out due to the answers in questions like here and here. In any case, I put it back in as action="upload_page.aspx" and that didn't work either. Commented Nov 10, 2014 at 17:38

1 Answer 1

1

There are some incorrect thing in code, like using onClientClick for server button click event . You can use PostedFile in this.uploadfile1.save...

Correct Complete code

ASPX Part

<form id="form1" runat="server" enctype="multipart/form-data" method="post" action="upload_page.aspx">
    <div>
        <asp:FileUpload ID="uploadfile1" runat="server" />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
        <asp:Button ID="btnReset" runat="server" Text="Reset" />
    </div>
</form>

Code behind

protected void btnUpload_Click(object sender, EventArgs e)
{
    if (uploadfile1.HasFile)
    {
        string rootpath = @"D:\webfile\";
        uploadfile1.PostedFile.SaveAs(rootpath + uploadfile1.PostedFile.FileName);
    }
}

Replace rootpath with required value.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. I made your changes but I do get an error, though. 'btnUpload_Click' is not a member of 'ASP_upload_page_aspx'
@noblerare, make sure you have single copy of btnUpload_Click , and clean, rebuild your solution, just to make sure , there is no compilation error.
I checked and double-checked everything but just couldn't get the .cs file to work. I ended up just putting the relevant code within <script> tags of the .aspx file and it worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.