// When File Submitted Call the API to process further. const handleFileChange = async…
- User
// When File Submitted Call the API to process further. const handleFileChange = async (event) => { const selectedFile = event.target.files[0]; if (selectedFile && selectedFile.type === "application/pdf") { setFile(selectedFile.name); setUploadLoading(true); setUploadSuccess(false); setUploadResponse(""); // Reset the response each time a new file is uploaded setUploadType("document"); // Set upload type to 'document' const formData = new FormData(); formData.append("file", selectedFile);
try { const response = await fetch(baseurl + "/upload-doc", { method: "POST", body: formData, }); // Check if the response is OK if (!response.ok) { alert("An error occured please try after some time..."); throw new Error("Network response was not ok" + response.text); } const reader = response.body.getReader(); // Create a reader from the response const decoder = new TextDecoder("utf-8"); let result = ""; while (true) { const { value, done } = await reader.read(); if (done) break; // Break the loop if done const chunk = decoder.decode(value, { stream: true }); result += chunk; // Append new chunk to the result setUploadResponse(chunk); } // Regular expression to find the last message without needing to trim const messagePattern = /Message\s*:\s*(\/[^ ]+|False)\s*$/gm; // Extracting the value directly without using 'Message:' const matches1 = result.match(messagePattern); const vdb = matches1 && matches1.length > 0 ? matches1[matches1.length - 1].replace(/.*:\s*/, "") : ""; // console.log(vdb); if (vdb === "False") { throw new Error("Failed to Upload the file."); } // Finalizing response setUploadSuccess(true); setFilename(selectedFile.name); setFilename(vdb); } catch (error) { console.error("Error uploading file:", error); setUploadSuccess(false); alert("File upload failed. Please try again after some time."); } finally { setUploadLoading(false); } } else if (selectedFile) { alert("Please upload a PDF file."); }};
If at any message has flase we need to detect it
- Assistant