Storing file paths in a database and images in a folder using ASP.NET, JavaScript, and effective methods

Seeking assistance in storing an image file in a folder and path in a database using asp.net and JavaScript. Can you provide guidance on how to retrieve the complete path of the image in JavaScript? I read about security reasons preventing access to the complete path, is there a way to work around this limitation?

Answer №1

To achieve the desired functionality, one can easily utilize the jQuery Forms plugin. It can be seamlessly integrated into asp webforms, as demonstrated below.

HTML / ASPX Page

<form id="myForm" action="fileupload.ashx" method="post"> 
    Name: <input type="text" name="name" /> 
    File: <input type="file" name="filetoupload" />
    <input type="submit" value="Submit Comment" /> 
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
 <script>
     $(document).ready(function () {
    // attach handler to form's submit event 
    $('#myForm').submit(function () {
        // submit the form 
        $(this).ajaxSubmit();
        // return false to prevent normal browser submit and page navigation 
        return false;
    });
});
</script>

This example showcases the use of pure HTML. In the case of aspx webforms, there will typically be only one form tag per page.

Include a WebHandler (.ashx extension) on the Website.

using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    HttpPostedFile MyFile=context.Request.Files["filetoupload"];
    if (MyFile.ContentLength > 0 && MyFile != null)
    {
        MyFile.SaveAs(context.Server.MapPath("Path/On/Server"));
    }
    context.Response.Write("Saved Successfully");
}

public bool IsReusable {
    get {
        return false;
    }
}

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

The powerful combination of Nuxt, Vuex, and Computed Properties allows for

Currently exploring Nuxt.js with Vuex, I have created a basic form containing an email field, a password field, and a button. All my state, mutations, and actions are functioning correctly. However, I encountered an issue when trying to create a computed ...

automatically dragging elements using jQuery UI

When implementing jquery UI draggable, I incorporate certain functionalities within the drag function. One specific task is scaling the dragging element based on its position. I am looking to automate the dragging of elements to specific coordinates (x, ...

The method expression does not match the Function type specified for the mongoose Model

Encountered a perplexing error today that has eluded my attempts at finding a solution. const mongoose = require('mongoose'); const userSchema = mongoose.Schema({ name: {type:String, required:false}, birthday: {type:String, required:f ...

Calculating the total of an array's values using JavaScript

Receiving information from an API and looking to aggregate the values it contains. Consider the following code snippet: function totalPesos(){ $http.get('/api/valueForTest') .then(function(data){ $scope.resumePesos = data.data.Re ...

What is the technique for performing asynchronous querying of multiple SQL databases?

Currently, I am in the process of developing a web application using nestjs and typeorm. I have been contemplating the functionality of the following code: const r1 = await this.connection.query(sqlA) const r2 = await this.connection query(sqlB) Does th ...

``Are you looking to create multiple canvases in a loop?

I've managed to get this command working exactly as I wanted: http://jsfiddle.net/m1erickson/64BHx/ However, I didn't anticipate how challenging it would be to turn the drawing into reality here: What I attempted to do is illustrated in this li ...

Exploring TypeScript Module Importation and WebPack Integration

Struggling with WebPack's injection of imported dependencies for a TypeScript project. The first challenge is getting TypeScript to recognize the imported module. In the header.ts file, there is a declaration of a module nested under vi.input, export ...

Is there a way for me to detect click events on Windows notifications?

Currently, I am attempting to send notifications through the node-notifier package. The goal is for a click on the notification to lead to a specific link. However, I am encountering difficulties in listening to the click event - the events provided by the ...

Issues with rendering HTML5 drag and drop styles are not visible on a Windows Server 2003 platform

I am currently developing a file upload tool utilizing Valum's Ajax-Uploader as the foundation. The concept is reminiscent of how attaching files works in Gmail. Users should be able to simply drag and drop a file from their desktop into the browser w ...

The fade effect on an element cannot be achieved using setTimeout

I'm trying to call a tooltip and hide it after 9 seconds, but for some reason, it's not working as expected. The issue I'm facing is that the "tooltip" only hides after it initially loads. It doesn't respond when I click on the "Touch ...

Utilize an imported function within a React component's return statement to avoid an invalid hook call issue

Hey everyone, I've created a reusable component that can translate keys into a chosen language using keynames as strings or variables. Typically, I would use a <t> tag for this purpose, but due to certain reasons, I am replacing current transl ...

The $().bind function will not function properly unless the document is fully loaded

Do I need to include $(document).ready() with $().bind? Here is the HTML portion: <head> <script type="text/javascript" src="jquery-1.10.2.min.js"></script> <script type="text/javascript" src=&quo ...

What is the mechanism behind $scope.$on activation and $destroy invocation?

Seeking an explanation on the functionality of $scope.$on and how $destroy works in two separate controllers. When switching routes, a new controller is invoked, leading to the activation of $destroy. Could someone shed some light on how $interval is in ...

What is the most efficient way to prevent duplicate items from being added to an array in a Vue 3 shopping cart

I currently have a functional shopping cart system, but I am facing an issue where it creates duplicates in the cart instead of incrementing the quantity. How can I modify it to only increment the item if it already exists in the cart? Also, I would like t ...

Why is the header image not appearing on mobile devices on the website?

I recently finished creating my own website with a header image that successfully expands to the width of the screen during testing on Google Chrome for mobile responsiveness. However, upon deploying and viewing the site on actual mobile devices, the heade ...

When the webpage is reloaded in Internet Explorer, a file is automatically downloading. How can this issue be resolved?

Below is the anchor tag in HTML for downloading a file. <a [href]="myFileUrl" class="u-text--document" download="myfile.csv"><span>Title of the Excel document (6.8MB)</span></a> This method is called on n ...

When assigning JSON to a class object, the local functions within the class became damaged

This is a demonstration of Object Oriented Programming in JavaScript where we have a parent Class called Book with a child class named PriceDetails. export class Book { name: String; author: String; series: String; priceDetails: Array<Price> ...

Having trouble parsing the BODY of a NodeJs JSON post request?

I've been working on creating a basic API using nodeJS, but I've run into a problem while trying to post data. Below is my app.js file: const express = require('express'); const feedRoutes = require('./routes/feed'); const ...

Is it possible to duplicate content from one div to another?

With over 90 divs, each containing an image or icon, I am looking for a way to display the clicked image in another div. I am not very familiar with JS yet, so I would appreciate a simple example using HTML5, Ajax, and js. EDIT: Just to clarify, the desti ...

Encountering an issue while trying to import the D3.js library into my React.js application

I'm having trouble with properly importing the d3.js libraries in my react.js app. I am using import * as d3 from 'd3' to import everything and store it in the d3 namespace, but I keep getting an error that says - Cannot read property ' ...