Upload Download File - potatoscript/asp.net.core.mvc GitHub Wiki

home

Upload File
Download File

You have to SHARE your upload folder in the server

home

Upload-File

  • Click a Upload button
<a style="height:25px;width:25px" onclick="showInPopup('@Url.Action("Upload", "Shared", null, Context.Request.Scheme)','Upload');"  
  class="btn btn-light">
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" style="margin-left:-10px;margin-top:-15px"
      fill="currentColor" class="bi bi-paperclip" viewBox="0 0 16 16">
      <path d="M4.5 3a2.5 2.5 0 0 1 5 0v9a1.5 1.5 0 0 1-3 0V5a.5.5 0 0 1 1 0v7a.5.5 0 0 0 1 0V3a1.5 1.5 0 1 0-3 0v9a2.5 2.5 0 0 0 5 0V5a.5.5 0 0 1 1 0v7a3.5 3.5 0 1 1-7 0V3z" />
      </svg>
</a>
  • Create a Model called UploadModel.cs
namespace TaskManagement.Models
{
    public class UploadModel
    {
        public IFormFile MyFile { set; get; }
    }
}
  • Controller to display the View
[Authorize]
        public IActionResult Upload()
        {
            return View(new UploadModel());
        }
  • Create the View called Upload.cshtml
@model UploadModel
@{
    ViewData["Title"] = "Upload";
    Layout = null;
}
    <form method="post" asp-action="Upload" onsubmit="return jQueryAjaxPost(this);">
        <input asp-for="MyFile" />
        <input type="submit" />
    </form>
  • Controller again for the Http Post after click on the submit button
        private readonly IHostingEnvironment hostingEnvironment;

        private String myPath;

        public UploadController(
            IConfiguration configuration, 
            IJSRuntime js,
            IHostingEnvironment environment)
        {
            _server = configuration;
            this.js = js;
            hostingEnvironment = environment;

            myPath = Path.Combine(hostingEnvironment.WebRootPath, "../../tms_doc");

        }

[HttpPost]
  public IActionResult Upload(UploadModel model)
  {
      //var file = model.MyFile;
      //String SavePath = Path.Combine(Directory.GetCurrentDirectory(),"wwwroot/doc",model.MyFile.FileName);

      //var uploads = Path.Combine(hostingEnvironment.WebRootPath,"doc");
      //String SavePath = Path.Combine(uploads, taskname + "_" + model.MyFile.FileName);

      String SavePath = Path.Combine(myPath, taskname + "_" + model.MyFile.FileName);

      using(var stream = new FileStream(SavePath, FileMode.Create))
      {
         model.MyFile.CopyTo(stream);
      }
     
      
      //you have to close the stream otherwise you will get the file in use error in download process
      stream.Close();

     return View();
  }

home

Download-File

 @{
    List<String> data = (List<String>)ViewData["File"];
    <h3 class="text-info pl-3">Download Documents :</h3>
    <div id="div_file">
      <table class="table_document">
         @for (int i = 0; i < data.Count; i++)
         {
           <tr><td>@Html.ActionLink(data[i],"Download", new { fileName = data[i]})</td></tr>
         }
       </table>
    </div>
  }
[HttpGet("download")]
        public IActionResult Download(String fileName)
        {
            //Build the File Path
            //String ReadPath = Path.Combine(Directory.GetCurrentDirectory(),  "doc", fileName);
            
            String ReadPath = Path.Combine(myPath, fileName);

            /*
            //Read the File data into Byte Array
            byte[] bytes = System.IO.File.ReadAllBytes(ReadPath);

            //Send the File to Download
            return File(bytes, "application/octet-stream", fileName);
            */

            var net = new System.Net.WebClient();
            var data = net.DownloadData(ReadPath);
            var content = new System.IO.MemoryStream(data);

            return File(content, "application/octet-stream", fileName);

        }
⚠️ **GitHub.com Fallback** ⚠️