-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.

noblerare
  • 10,277
  • 23
  • 78
  • 140
  • Where's the `action` for the `form`? – emerson.marini Nov 10 '14 at 17:29
  • Actually, you have nothing in your `form` to `submit` it. – emerson.marini Nov 10 '14 at 17:30
  • Well, I am confused because I had taken it out due to the answers in questions like [here](http://stackoverflow.com/questions/569565/uploading-files-in-asp-net-without-using-the-fileupload-server-control) and [here](http://stackoverflow.com/questions/3167240/asp-net-file-upload). In any case, I put it back in as `action="upload_page.aspx"` and that didn't work either. – noblerare Nov 10 '14 at 17:38

1 Answers1

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.

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • 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 Nov 10 '14 at 18:22
  • @noblerare, make sure you have single copy of `btnUpload_Click` , and clean, rebuild your solution, just to make sure , there is no compilation error. – Arindam Nayak Nov 11 '14 at 05:43
  • 1
    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 ` – noblerare Nov 12 '14 at 17:25