1

This is my code for Download a File in my MVC App. Im having a weird error and I cant a way to put it right. The error is > Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.FileResult'

What I want with this code is, that while theres a file download it, but if theres not file(null) then it just dont do nothing or dont return nothing

 public FileResult Download(string Doc)
 {

    string fullPath = Path.Combine(Server.MapPath("~/UploadFiles/"), Doc);

    if (fullPath == null)
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);

        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Doc);
    }

        return RedirectToAction("List");
         }
tereško
  • 58,060
  • 25
  • 98
  • 150
Agar
  • 13
  • 2
  • 11
  • No, Raphael. It is not the same. – Agar Sep 22 '17 at 19:21
  • If you think that your question was wrongfully closed as duplicate, then *you* have to edit your question and explain *why* it is different. – honk Sep 24 '17 at 07:44

1 Answers1

1

Change your action method return type to ActionResult

Both RedirectResult and FileResult are inheriting from this ActionResult abstract class.

Also your if condition does not makes sense! It will always be false as the Path.Combine will give you a non null string value. May be you want to check the file exist in the disk ?

public ActionResult Download(string Doc)
{    
    string fullPath = Path.Combine(Server.MapPath("~/UploadFiles/"), Doc);

    if (System.IO.File.Exists(fullPath))
    {
        byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);

        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Doc);
    }
    return RedirectToAction("List");
}

Your code should be compilable now.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • With that, my code doesn't download anything or return null – Agar Sep 22 '17 at 19:17
  • That has nothing to do with the return type change. Put a breakpoint in your method and see whether it is running as expected. – Shyju Sep 22 '17 at 19:18
  • public ActionResult Download(string Doc) { string fullPath = Path.Combine(Server.MapPath("~/UploadFiles/"), Doc); if (fullPath == null) { return RedirectToAction("List"); } byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath); return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, Doc); } – Agar Sep 22 '17 at 19:30
  • I put it that way, it does the downloading, but when theres not file it crushed – Agar Sep 22 '17 at 19:31
  • See my updated answer. Your if condition was wrong. – Shyju Sep 22 '17 at 19:31
  • With your code it does the downloading but when theres no file Value cannot be null. Parameter name: path2 and I dont want this error. – Agar Sep 22 '17 at 20:00
  • TBH, i never returned with the content type `MediaTypeNames.Application.Octet`. What sort of files are you trying to return ? – Shyju Sep 22 '17 at 20:01
  • Im returning PDFs File – Agar Sep 22 '17 at 20:02
  • yea. It should work. I just verified it locally. What error are you getting ? – Shyju Sep 22 '17 at 20:08
  • Value cannot be null.\r\nParameter name: path2" – Agar Sep 22 '17 at 20:11
  • string fullPath = Path.Combine(Server.MapPath("~/UploadFiles/"), Doc); Because the object Im calling has info but doesnt have file in it – Agar Sep 22 '17 at 20:11
  • is the value of `Doc` null ? you get that exception from `Combine` method when the value of `Doc` is null – Shyju Sep 22 '17 at 20:15
  • Yes, Doc is null. I dont want getting that error if Doc is null. I which the controller dont do nothing while clicking dowloading – Agar Sep 22 '17 at 20:22
  • do a null on doc before entering your other if condition. If the value of Doc is null, return some other result ( The RedirectResult ?? or an error message back to the user) – Shyju Sep 22 '17 at 20:23
  • Yes, that's what I want. Let me work it out. Thanks. I'll reply in a second. – Agar Sep 22 '17 at 20:25
  • If I do this: if (Doc == null) { return View("List"); } then it gaves me an error Value cannot be null. Parameter name: source. Source Error: Line 49:
    Line 50: Line 51: @Html.Grid(Model).Columns( Line 52: Columns => Line 53: {
    – Agar Sep 22 '17 at 20:32
  • I thought it was juts going to stay at the same page but no. :s xD – Agar Sep 22 '17 at 20:33
  • It will not. because this method will be executed when user clicked on the link from some other page and reach here. So wherever you are rendering those links, you can check the existence of a file in a efficient manner and show only those links – Shyju Sep 22 '17 at 20:34