Open anishcana opened 6 months ago
<!DOCTYPE html>
<!DOCTYPE html>
import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile;
@RestController public class FileUploadController {
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "Please select a file to upload.";
}
// Process the file (e.g., save to disk, parse Excel)
// You can use Apache POI or similar libraries to handle Excel files
return "File uploaded successfully.";
}
}
// upload.js $(document).ready(function() { $("#uploadForm").submit(function(event) { event.preventDefault(); var formData = new FormData($(this)[0]); uploadFile(formData); }); });
function uploadFile(formData) { $.ajax({ url: "/upload", type: "POST", data: formData, contentType: false, processData: false, success: function(response) { // Handle success console.log("File uploaded successfully:", response); }, error: function(xhr, status, error) { // Handle error console.error("Error uploading file:", error); } }); }
load Excel File
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Upload</button>
// upload.js $(document).ready(function() { $("#uploadButton").click(function() { $("#fileInput").click(); });
$("#uploadForm").submit(function(event) {
event.preventDefault();
var formData = new FormData($(this)[0]);
uploadFile(formData);
});
});
<!DOCTYPE html>
ependency>
<artifactId>validation-api</artifactId>
</dependenc
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;
@RestController public class YourController {
@PostMapping("/createRecord")
public ResponseEntity<String> createRecord(@RequestBody YourDTO formData) {
// Code to save the formData to the database
// You can use your service layer to handle this
// Return appropriate response
return ResponseEntity.ok("New record created successfully!");
}
}
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController;
@RestController public class YourController {
@PostMapping("/editRecord")
public ResponseEntity<String> editRecord(@RequestBody YourDTO formData) {
// Code to update the existing record in the database
// You can use your service layer to handle this
// Return appropriate response
return ResponseEntity.ok("Record edited successfully!");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html>
<!DOCTYPE html>
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController;
import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;
@RestController public class ZipController {
@Value("${source.file.path}")
private String sourceFilePath;
@Value("${destination.zip.path}")
private String destinationZipPath;
@PostMapping("/zip")
public void zipFiles() throws IOException {
FileOutputStream fos = new FileOutputStream(destinationZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
zipFile(sourceFilePath, sourceFilePath, zos);
zos.close();
}
private void zipFile(String fileToZip, String fileName, ZipOutputStream zos) throws IOException {
File file = new File(fileToZip);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
}
source.file.path=/path/to/your/source/file destination.zip.path=/path/to/your/destination/zipfile.zip
<!DOCTYPE html>
// Assume this is the data received from the backend var responseData = [ { name: "File 1", directory: "/path/to/directory1" }, { name: "File 2", directory: "/path/to/directory2" }, // Add more data as needed ];
// Function to populate the table with data function populateTable(data) { var tbody = $('#dataTable tbody'); tbody.empty(); // Clear existing table body content
}
// Call the function to populate the table with the received data populateTable(responseData);