views:

629

answers:

3

I am trying to implement the checkscript feature of uploadify in an asp.net mvc view but i can't determine what the key is i should be using on the controller side. Below is the php script but i am not very familiar with php and can't determine what php is scraping out of the httprequest. Has anyone implemented this? The documentation is a little sparse (as in nonexistent).

$fileArray = array();
foreach ($_POST as $key => $value) {
    if ($key != 'folder') {
     if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_POST['folder'] . '/' . $value)) {
      $fileArray[$key] = $value;
     }
    }
}
echo json_encode($fileArray);
?>
A: 

Here is the solution for anyone searching. Basically the uploadify script sends the file names and a unique key it generates in the forms collection. You can get at it by iterating through allkeys. The controller action below iterates through the form allkeys collection and if the key is not folder (uploadify folder parameter is also passed in the formscollection for scriptCheck) it checks to see if the file already exists. If it does exist the key and value are added to a dictionary which is then returned to the client. The uploadify plugin will then alert the user that the file exists and give them an opportunity to cancel the upload. Hope this helps someone else out.

public ActionResult FileExists(FormCollection forms)
    {
        Dictionary<string,string> fileArray = new Dictionary<string,string>();

        foreach (string key in forms.AllKeys)
        {
            if (key != "folder")
            {
                string targetDirectory = Path.Combine(Request.PhysicalApplicationPath, @"uploads\documents\");
                string targetFilePath = Path.Combine(targetDirectory, forms[key]);
                if (System.IO.File.Exists(targetFilePath)) fileArray.Add(key, forms[key]);
            }
        }

        return Json(fileArray);
    }
Kevin McPhail
A: 

What is Json in asp.net.. have you referenced a separate library etc as i can't seem to find out how to access that keyword.. cheers, Tom

Tom
JSON is an asp.net mvc actionresult return type. It serializes to javascript object notation (JSON). If you are still using asp.net webforms you will need to find a json serializer to use. I am sure a google or SO search will get you started.
Kevin McPhail
A: 

How do you call that ActionResult (FileExists) with uploadify?