To send a POST request with a file in TypeScript, you can use the http
module from Angular's @angular/common/http
package. You also need to use a library such as Multer to handle file uploads in Node.js.
Here's an example code snippet:
index.ts379 chars18 lines
Here, we first create a new FormData
object and append the file to it with the append
method. Then, we send a POST request to the server with the HttpClient
's post
method, passing in the URL and the FormData
object as the request body.
On the server side, you can use Multer to handle the uploaded file. Here's an example of how to use Multer with Express:
index.tsx238 chars10 lines
Here, we create an Express app and set up a Multer middleware with the upload.single()
method. This middleware will handle a single file upload with the file
field name. In the route handler, you can access the uploaded file with req.file
. You can then process the file as needed (e.g. save to disk, store in a database, etc.).
gistlibby LogSnag