Verify the Javascript for file upload in ASP.NET folder prior to uploading it

Struggling with this problem for days now, I could really use some fresh perspective.

My setup includes Windows Server 2012, IIS 8.0, and ASP.NET 4.5. I'm new to both IIS and ASP.NET, so please bear with me. The website I'm working on involves file uploads, which need to be validated before being stored on the server.

Initially, I attempted to use Javascript to validate the inputs before submission, but nothing gets uploaded. To simplify the process, I decided to try a basic upload without validation for now.

Here's the current state of the files:

upload_page.aspx

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

        if(filter.test(file1))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
</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);
    }
}

Any suggestions on where I might be going wrong? Your help would be greatly appreciated. Thank you.

Answer №1

Code contains some errors, such as using onClientClick for server button click event. Instead, use PostedFile in this.uploadfile1.save...

Here is the 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);
    }
}

Make sure to replace rootpath with the necessary value.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Visitors may not immediately notice the CSS/JavaScript modifications in their browsers

Currently, I am working on a web application utilizing Spring MVC and Hibernate, deploying it on Apache Tomcat 8. Most of my users access the app using Google Chrome. However, I have encountered an issue where whenever I update the CSS or JavaScript files ...

navigating through a specific section of a data chart

I need the first column of a table to stay fixed while the rest of the columns scroll horizontally. Here's the code I have: <div id="outerDiv"> <div id="innerDIv"> <table> <tr><td>1</td><t ...

The drop-down menu is failing to display the correct values in the second drop-down

Having trouble with the update feature in this code. There are 2 textboxes and 2 dropdowns, but when selecting a course code, the corresponding values for the subject are not being posted. Can anyone assist? view:subject_detail_view <script type="te ...

Top technique for mirroring a website

Recently, I created a directory for a client's website. The site is fully operational, but the client would like a replicated version for testing and learning purposes. To create a full replica of his website, it will require hours of work to change ...

Iterating through an array of objects within a Vuejs template

Consider the JSON data below: { "games": [ { "id": 1, "init_date": "2020-02-11T07:47:33.627+0000", "players_in_game": [ { "id": 1, "player": { "id": 1, "player": "Jack Bauer", ...

Display sqllite database information in an HTML view using Ionic 2

After executing a select query against the database, I am able to read the results from the logcat and view the data there. However, I am encountering an issue where the data is not rendering in the HTML view after binding. //data retrieve section db.exec ...

How can I dynamically pass a background:url using JavaScript to CSS?

I have a CSS code snippet below that I would like to dynamically change the "background:url" part using Javascript. div{ background: url('pinkFlower.jpg') no-repeat fixed; -webkit-background-size: cover; -moz-background-size: cover; ...

What could be causing the malfunction of the .setAttribute() and .removeAttribute functions in my JavaScript program?

Below is a flashcard game I have developed: Once a user enters the correct answer in the text box, "Correct!" is expected to be displayed in bold green font. The CSS attributes are then supposed to be removed shortly after. Here is the HTML code for the i ...

ReactJS: Error in syntax detected in src/App.js at line 16, column 6. This token is unexpected

Just starting out with reactjs and encountered a simple example in a tutorial. Ran into a syntax error when trying to return html tags. Below is the error that popped up: ./src/App.js Syntax error: C:/Users/react-tutotial/src/App.js: Unexpected token ...

Tips for validating a form with the assistance of jQuery

While there are multiple answers out there for this particular question, I am specifically interested in validating an entire form with dynamic input fields sourced from a database similar to Google Forms. HTML <div class="rs_card"><spa ...

Steps to eliminate a choice from the MUI Datagrid Column Show/Hide feature

I am attempting to customize which columns are displayed on the <GridToolbarColumnsButton/> component within the MUI Datagrid toolbar (refer to the image below) https://i.stack.imgur.com/joZUg.jpg Potential solution: I have been exploring the AP ...

Sample using Solrnet with ASP.NET without MVC

I'm currently exploring Solrnet and how to integrate it with an ASP.NET website. I noticed that the sample application available on the code repository is MVC based. Does anyone happen to know of a version for plain vanilla ASP.NET? Appreciate any he ...

Obtaining data from a Nested Json file in Angular 5

Encountering difficulties retrieving data from nested JSON. Error: 1. <h3>{{sampledata}}</h3> displaying [object Object] 2. <p>{{sampleDataModel.Title}}</p> ERROR TypeError: Cannot read property 'Title' of undefined ...

Material UI Table dynamically updates with Firestore real-time data

My current code aims to update a Material UI table in real-time with data from a Firestore database. I can fetch the data and store it in an array, but when I assign this array to the table data, nothing changes. I've heard that I should use states fo ...

Exploring the process of passing parameters in Material-UI React styled components

Recently, I developed a new component const CategoryDialog = (props) => { const classes = useStyles(); console.log(props); return ( <div> <Dialog fullScreen open={props.dilaogOpenProp} TransitionCompone ...

Running a jQuery function that triggers a PHP script when the page

Hi there! I'm currently working on implementing jQuery's ajax feature to call a php script when the page loads. This php script will fetch xml data from a web service URL, parse it, and then display specific parts of it within a div tag on the pa ...

Loading content dynamically through asynchronous JavaScript and XML (AJAX) instead of relying on traditional page methods

Currently, I am in the process of developing a website that relies on page methods to load its browser popups. While this method is functional, it is becoming increasingly disorganized. I typically associate page methods with more simplistic tasks, such as ...

Converting a JavaScript string containing an `import` statement into a browser-compatible function

Can my vue application transform the given string into a callable function? const test = 'import { pi } from "MathPie"; function test() { console.log(pi); } export default test;' The desired output format is: import { pi } from "M ...

Ionic ion-view missing title issue

I'm having trouble getting the Ionic title to display on my page: http://codepen.io/hawkphil/pen/oXqgrZ?editors=101 While my code isn't an exact match with the Ionic example, I don't want to complicate things by adding multiple layers of st ...

Passing variables from AJAX response to PHP using a passthru function

In my PHP file, I have implemented a functionality where certain checkboxes are checked and upon clicking a button, a JavaScript file is triggered. The JavaScript code snippet used is as follows: $.ajax ({ type: "POST", url: "decisionExec.php", ...