Send a file using XMLHttpRequest and include additional post data

I was looking on Google for information on how to send a post to an ashx page with both a file (for uploading) and some post parameters like the file's name.

Does the XMLHttpRequest send(...) method accept both datafile and string in the same call?

I am using Ajax to make this call.

Thanks.

Answer №1

Give this a shot:

let request = new XMLHttpRequest();
request.open('post', url, true);
request.setRequestHeader("Content-Type","multipart/form-data");

let formData = new FormData();
formData.append("thefile", file);
formData.append('stringParam',param1);
request.send(formData);

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

What's the process for creating a synchronous function in AngularJS?

Is there a way to make storyboard.getAdressTimeLine run synchronously? I need storyboard.drawTimeLine to continue executing only after storyboard.getAdressTimeLine is done for (var i = 0; i < response.data.length; i++) { var obj=response.data[i]; var d ...

Error message: The method app.get() is invalid as it is not a

I'm having trouble using Express in a prototype function ioServer() { } module.exports = ioServer; ioServer.prototype.start = function() { var app = require('express') var http = require('http').Server(app) ...

How can a User Control in ASP.NET generate a new CSS class dynamically?

Within my UserControl, there is an AJAX modal popup extender that is causing me some trouble: <ajax:ModalPopupExtender ID="MPE" runat="server" TargetControlID="btnChangePassword" PopupControlID="pnlPasswordChanging" BackgroundCssClass="modalBackgrou ...

Employing pNotify as a seamless substitute for the traditional JavaScript confirmation pop-up

I have successfully implemented inline JavaScript confirmations and also as a function. However, I am struggling to figure out how to integrate PNotify confirmation dialog. My goal is to replace the existing confirm('Sure?') with pconfirm('S ...

At times, the Kendo UI Tooltip may linger on screen longer than expected, failing to disappear as

I've been experimenting with this issue for quite some time now, but I'm stumped. Whenever I quickly move my mouse cursor over a series of links, occasionally a tooltip will linger on the screen even after the cursor has moved away from the link. ...

Retrieve the domain name of the remote origin with Cross-Origin Resource Sharing (C

Currently, I am making an Ajax call from a different domain. My goal is to restrict this call to only specific domains. I'm wondering what the most effective method is for obtaining the remote origin from HttpServletRequest. Can we rely on request.get ...

Servlet errors and conditions

As I embark on my first venture into writing a JAVA servlet, a question has arisen in my mind. It's worth noting that my servlet will be accessed through Google Web Toolkit using AJAX. The initial step I take is to initialize a PrintWriter and begin ...

Error: You forgot to close the parenthesis after the argument list / there are duplicate items

I have already tried to review a similar question asked before by checking out this post, but unfortunately, I still couldn't find a solution for my problem. Even after attempting to add a backslash (\) before the quotation mark ("), specificall ...

When making recursive AJAX calls, the script that is included between each recursion is not being executed

My recursive Ajax call is functioning correctly (the PHP script is doing its job, recursion is working, everything is fine) EXCEPT that in between the ajax calls, I am trying to update an input text value to show the progress, but it only updates once the ...

Implementing asynchronous node.js output on Azure platform

Although I usually work with different programming languages, I find myself needing to incorporate some node.js code, which is entirely new to me. Currently, my goal is to have the outcome of my azure function app reliant on calling an API. To achieve this ...

How to handle custom errors in ASP.NET WebApi 5 by throwing HttpResponseException with incorrect status code

When using WebAPI / REST, you have the ability to send personalized error messages along with a standard Status Code. For example, you can send a 402 - Not found status code along with your own message in the header or body. This feature works perfectly f ...

Could not locate module: The package path ./react is not exported from the package in E:NextAppportfolio_website-mainportfolio_website-main ode_modules ext-auth

I am encountering an issue while trying to import SessionProvider from Next-Auth. The error message that is being displayed is: "Module not found: Package path ./react is not exported from package E:\NextApp\portfolio_website-main\port ...

Striving to get $(document).ready to function seamlessly with load()

While attempting to load my page using the load function, I encountered an issue with initializing the orbit slider from Foundation. The images load fine, but the slider fails to initialize. Here is the code used to load slider.html: $("a#1").click( func ...

Saving base64 encoded pdf on Safari

I am facing an issue with a POST call that returns a base64 PDF. The problem arises when trying to convert it to a Blob and download it using Safari browser. This method works perfectly in all other browsers. openPdf = () => { const sendObj = { ...

Twitter typeahead with a dynamic array of strings

Currently, I am making an ajax call to retrieve a list of objects. Once this array of objects is filled, a separate string[] is created with the names of these objects. My goal is to pass this data to Twitter Typeahead using a Bloodhound source. However, ...

What is the process for inserting a document into an array that is nested within another array?

My current dilemma revolves around a document (referred to as 'root') which contains an array of other documents ('stick'), each of which in turn contain another array of documents ('leaf'). Simply put: root{ stickChain[leaves ...

Tips for incorporating external AMD modules with Angular2 using WebPack

In my current project using Angular2, TypeScript, and WebPack, I am looking to incorporate the Esri ArcGIS JavaScript API. The API can be accessed from the following URL: By importing Map from 'esri/Map';, I aim to import the corresponding modul ...

Utilize Google Drive and scripts to incorporate map images into a React application

I'm currently working on setting up an album feature on my react website for a friend, and I would like the images in the album to be linked to a Google Drive so that he can easily upload new images whenever he wants. After successfully inserting the ...

ASP.NET is being used to host an angular application, but the deployUrl feature is

I have a .NET 6.0 ASP.NET application where I've set up a route /ngxapp to serve my Angular 13 application. The Angular app is stored in the wwwroot/ngxapp folder, as shown below. I've been using this code for years to deliver the Angular app: ...

Altering the href text within a script causes the text to disappear instead of updating

Function Triggered by Button Click: function LoadEnglishText() { document.getElementById("txt_whatwedo_learnmore2").innerHTML = "here."; } HTML Link to be Updated: <a id="txt_whatwedo_learnmore2" href="./pdf/Pricing_App_Dev_2019_Ger.pdf">hier ...