ivandpf
ivandpf2mo ago

PHP to Javscript little code

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_FILES["file"])) {
$file= $_FILES["file"];
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_FILES["file"])) {
$file= $_FILES["file"];
}
}
Which is the equivalent of that code to Javscript? I have atm that
export async function csvMarcatges({ response, request }: Context) {
if (request.method === "POST") {
response.status = 200;
response.body = { message: "Doing Post" };

let file =
}
}
export async function csvMarcatges({ response, request }: Context) {
if (request.method === "POST") {
response.status = 200;
response.body = { message: "Doing Post" };

let file =
}
}
3 Replies
Deno AI Helper
Deno AI Helper2mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the PHP code you provided, it looks like you're trying to handle a file upload in a POST request. In the JavaScript (TypeScript) snippet you provided, you've started to define a function that handles a POST request, but it's incomplete. You need to retrieve the file from the request. In Deno, you can do this using the request.body() method, which can handle different types of request bodies, including form data for file uploads. Here's a rough outline of how you might handle a file upload in Deno, but please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given:
export async function csvMarcatges({ response, request }: Context) {
if (request.method === "POST") {
const body = await request.body().value;
// Assuming the file is sent as a form-data field named 'file'
const file = body.get('file');
if (file) {
// Handle the file here
response.status = 200;
response.body = { message: "File received" };
} else {
response.status = 400;
response.body = { message: "No file uploaded" };
}
}
}
export async function csvMarcatges({ response, request }: Context) {
if (request.method === "POST") {
const body = await request.body().value;
// Assuming the file is sent as a form-data field named 'file'
const file = body.get('file');
if (file) {
// Handle the file here
response.status = 200;
response.body = { message: "File received" };
} else {
response.status = 400;
response.body = { message: "No file uploaded" };
}
}
}
Please note that the Context type and how you handle the request and response might vary depending on the framework you're using with Deno (if any). The above code assumes that request.body() returns a form-data object with a get method, which is not standard in Deno's built-in HTTP server API. You might need to use a third-party library to parse form-data or handle file uploads more gracefully. Make sure to consult the documentation of the specific framework or library you're using
Leokuma
Leokuma2mo ago
const formData = await request.formData();
let file = formData.get('file') as File;
const formData = await request.formData();
let file = formData.get('file') as File;