Question

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);
?>
Was it helpful?

Solution

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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top