Hi
I want to create Dynamic Upload Documents option.
Thanks
Hi
I want to create Dynamic Upload Documents option.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Deepak RawatRank #646 · 3.7K reputationPosted Sep 1, 2024, 9:41 AM EDT
Try this
private setupUploaders() { const uploaderFields = this.getUploaderFields(); uploaderFields.forEach((uploaderField) => { // must be an array since form can have multiple uploaders this.uploader[uploaderField] = new FileUploader({url: ENDPOINT + '/upload/' + uploaderField, itemAlias: 'photo'}); this.uploader[uploaderField].onAfterAddingFile = (file) => { file.withCredentials = false; }; this.uploader[uploaderField].onCompleteItem = (item: any, response: any, status: any, headers: any) => { const data = JSON.parse(response); if (status === 200) { console.log(item); console.log(data.type); // this.baseForm.get('') } else { this.message = { type: 'danger', message: 'Uploaded failed :' + data.message, title: 'Error: ', status }; } }; }); } backend.. import {UploadController} from "../controllers/upload.controller"; export class UploadRoutes { public uploadController: UploadController = new UploadController() public routes(app): void { app.route('/upload/:type').post(this.uploadController.process) } } controller import { Request, Response } from 'express'; import {_} from 'lodash'; import * as Resize from '../libraries/Resize'; export class UploadController{ public process(req: Request, res: Response){ //req.params.type == type is pass as response back to angular Resize.resize(req, res,req.params.type); // inside the Resize class.. snippet below.. // response.type = type; // res.send(response) } }Aman GuptaRank #193 · 12.4K reputationPosted Sep 2, 2024, 3:04 AM EDT
Hi Ramco,
To create a dynamic document upload option in a .NET application, you can use ASP.NET MVC or ASP.NET Core MVC. The idea is to allow users to upload multiple documents with the flexibility to add or remove file input controls dynamically.
Steps to Implement a Dynamic Document Upload Option:
1. Create the Model: Define a model to hold the uploaded file data.
public class DocumentUploadModel { public List Files { get; set; } = new List(); }2. Create the View: Create a view that allows users to dynamically add or remove file input fields.
@model DocumentUploadModel Add Another Document document.getElementById("addFileInput").addEventListener("click", function() { var fileInputs = document.getElementById("fileInputs"); var newInput = document.createElement("div"); newInput.className = "file-input"; newInput.innerHTML = ''; fileInputs.appendChild(newInput); });3. Create the Controller: Implement the controller to handle the form submission and save the uploaded files.
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.IO; using System.Threading.Tasks; public class DocumentsController : Controller { private readonly IWebHostEnvironment _environment; public DocumentsController(IWebHostEnvironment environment) { _environment = environment; } [HttpGet] public IActionResult Upload() { return View(new DocumentUploadModel()); } [HttpPost] public async Task UploadDocuments(DocumentUploadModel model) { if (model.Files != null && model.Files.Count > 0) { foreach (var file in model.Files) { if (file.Length > 0) { var filePath = Path.Combine(_environment.WebRootPath, "uploads", file.FileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } } } return RedirectToAction("UploadSuccess"); } return View("Upload"); } public IActionResult UploadSuccess() { return View(); } }4. Set Up the Environment: Ensure that the necessary directory (e.g., wwwroot/uploads) exists for saving uploaded files.
Explanation:
Advanced Features:
This setup gives you a flexible and dynamic document upload solution in .NET, suitable for various applications.