Deb
Deb

Reputation: 991

Image Loading Through Controller Issue

My Controller does not get hit and i get a 400 error when i am trying to load images through a controller as follow.

var purl = rootPath + "Fupload/EmpImg/" + $("#photofile").val();
$("#ephoto").attr("src",  purl);

However if i remove filename part (i.e. $("#photofile").val() ) from url then the controller gets hit.

here is my action method

public FileResult EmpImg(string id)  // id has filename
{
   .....

    String pth = ConfigurationManager.AppSettings["UploadPath"];
    pth = Path.Combine(pth, "EmpImg");

    var dir = pth;
    var path = Path.Combine(dir, id );

    if (System.IO.File.Exists(path) == false)
        return null;

    ------
    ---
}

Since the file name comes from database as json , i need to update img src through javascript as shown above. I have to pass filename to the action method and when i do it does not get hit.

How do i get this to work?

thanks.

Upvotes: 0

Views: 38

Answers (1)

JamieD77
JamieD77

Reputation: 13949

try using url helper.. not sure what rootpath is.. but

var purl = "@Url.Action("EmpImg","Fupload")";
$("#ephoto").attr("src",  purl + '?' + $.param({id: $("#photofile").val()}));

Upvotes: 1

Related Questions