0

THIS IS MY FILE UPLOAD AND DOWNLOAD CODE

 [HttpPost]
    public ActionResult Save(Rent Rent , FileUpload upload, HttpPostedFileBase file)
    {

        if (Rent.Id == 0)
            _Context.Rent.Add(Rent);

        else
        {
            var rentInDb = _Context.Rent.Single(c => c.Id == Rent.Id);

            rentInDb.tenantId = Rent.tenantId;
            rentInDb.unitId = Rent.unitId;
            rentInDb.startDate = Rent.startDate;
            rentInDb.endDate = Rent.endDate;
            rentInDb.Amount = Rent.Amount;
            rentInDb.leaseStatus = Rent.leaseStatus;


        }

            _Context.SaveChanges();

        var rent = _Context.Rent.Single(r => r.Id == Rent.Id);

        var up = Request.Files["file"];
        if (up.ContentLength > 0) {



            var fileName = Path.GetFileName(file.FileName);
            var guid = Guid.NewGuid().ToString();
            var path = Path.Combine(Server.MapPath("~/uploads"), guid + fileName);
            file.SaveAs(path);
            string fl = path.Substring(path.LastIndexOf("\\"));
            string[] split = fl.Split('\\');
            string newpath = split[1];
            string imagepath = "~/uploads/" + newpath;
            upload.length = imagepath;
            upload.Rent = rent;
            _Context.FileUpload.Add(upload);
            _Context.SaveChanges();

        }
        return RedirectToAction("leaseStatus", "Home");
    }


 public ActionResult Downloads(int id)
    {
        var fl = _Context.FileUpload.Where(f => f.rentId == id);
        var up = Request.Files["file"];




        return View(fl );
    }

public FileResult Download(string ImageName)
  {
          var FileVirtualPath = "~/uploads/" + ImageName;

        return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));


}

THIS IS MY VIEW !!

@model IEnumerable<mallform.Models.FileUpload>
@{
    ViewBag.Title = "Downloads";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Downloads</h2>


@foreach (var file in Model)
{
   

    

    <a href=@file.length target="_blank">Download</a>

}

THIS SHOWS STANDARD 404.0 AND SOMETIME hidden element error :( Please help. In my code it includes the upload a file code, then there Is a download action which leads me to download view and In download view, I have a link to download the file by file result. But It always shows me an error. Please tell me if there is an issue with the path or what is going on?

  • Possible duplicate of [Download file of any type in Asp.Net MVC using FileResult?](https://stackoverflow.com/questions/3604562/download-file-of-any-type-in-asp-net-mvc-using-fileresult) – TAHA SULTAN TEMURI Jun 29 '18 at 05:04
  • Your ActionMethod `Download(string ImageName)` is taking `imageName` as param and in ` – mmushtaq Jun 29 '18 at 05:11
  • Thanks man but now it says : Could not find a part of the path 'C:\Users\Administrator\Documents\mall-90f203a01a995e8b804a90b7e188646c7cc895c3\mallform\uploads\~\uploads\a5ff7121-2c1c-451e-9c8c-e6d69a5ccf751st image.jpg'. – Vikram Sharma Jun 29 '18 at 05:31
  • Use `var FileVirtualPath = Server.MapPath("~/uploads/" + ImageName);` – mmushtaq Jun 29 '18 at 05:37
  • same could not find path error – Vikram Sharma Jun 29 '18 at 05:44

2 Answers2

0

File Result Action should be like this.

public FileResult Download(string ImageName)
{

    byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(@"~/uploads/"+ImageName));
   string fileName = "myfile."+Path.GetExtension(Server.MapPath(@"~/uploads/"+ImageName));
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);


}
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
0

Error Code 404 means the url is not found by your anchor tag. https://en.wikipedia.org/wiki/HTTP_404

You need to pass ImageName parameter from your anchor tag to the controller. You can do something like this:

View

@model IEnumerable<mallform.Models.FileUpload>
@{
    ViewBag.Title = "Downloads";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Downloads</h2>


@foreach (var file in Model)
{

    @Html.ActionLink(
          "Download File",              // Anchor Text
          "Download",                   // Action Name 
          "Controller Name",            // Controller Name
           new {                 
                 ImageName= "Pass the value of imagename parameter"
               },
           null         // Html Attributes
          )
}

Controller

    public FileResult Download(string ImageName)
      {
     //var FileVirtualPath = "~/uploads/" + ImageName; 

    //If using Physical Path  
    //var FileVirtualPath = HttpContext.Current.Request.MapPath("~/uploads/" + ImageName); 

    //If using Virtual Path
     var FileVirtualPath = HttpContext.Current.Server.MapPath("~/uploads/" + ImageName);   

     return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));

    }
Sahil Sharma
  • 1,813
  • 1
  • 16
  • 37
  • thanks man , it has recognized the action name but now it says : Could not find a part of the path 'C:\Users\Administrator\Documents\mall-90f203a01a995e8b804a90b7e188646c7cc895c3\mallform\uploads\~\uploads\a5ff7121-2c1c-451e-9c8c-e6d69a5ccf751st image.jpg'. – Vikram Sharma Jun 29 '18 at 05:30
  • Because the image url is not correct. Is it the actual path to the image 'C:\Users\Administrator\Documents\mall-90f203a01a995e8b804a90b7e188646c7cc895c3\mallform\uploads\~\uploads\a5ff7121-2c1c-451e-9c8c-e6d69a5ccf751st image.jpg' – Sahil Sharma Jun 29 '18 at 05:38
  • 'C:\Users\Administrator\Documents\mall-90f203a01a995e8b804a90b7e188646c7cc895c3\mallform\uploads\a5ff7121-2c1c-451e-9c8c-e6d69a5ccf751st image.jpg'vvv actual path should be this but it shows uploads twice man what to do ? – Vikram Sharma Jun 29 '18 at 05:43
  • @VikramSharma - Updated the answer. Check now – Sahil Sharma Jun 29 '18 at 05:53
  • I will mark you as answer man , thank you all I did is removed the whole path in my file result method var FileVirtualPath = "~/uploads/" + ImageName; as : var FileVirtualPath = "" + ImageName; and the file got downloaded – Vikram Sharma Jun 29 '18 at 05:54
  • Great. Cheers :) – Sahil Sharma Jun 29 '18 at 05:58