This is a migrated thread and some comments may be shown as answers.

[Solved] Upload binding issue

2 Answers 119 Views
Upload
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jacob
Top achievements
Rank 1
Jacob asked on 06 Jun 2011, 11:56 PM
Hi there quys

Need some help, know what I am doing might not be the correct solution and it will end up biting me where I don't want it to....
Right here is the scenario, using a standard wired up create view to a client model. This model contains a field that holds the path to the client photo, do not want to store the photo as a blob in the db, due to requirements issues.

I am using the telerik uploader ofcourse, based on the standard examples posted, I need to get the uploaded path back into the model, the uploader doesn't support binding right or am I just missing it somewhere? here is the view code:

@(Html.Telerik().Upload()
                    .Name("clientUpload")
                    .Async(async => async
                            .Save("UploadFile""Upload")
                            .Remove("Remove""Upload")
 
                            )
                    .Multiple(false)
                    .ClientEvents(events => events
                                    .OnSuccess("onSuccess")
                                                                
                                  )
                                  
 
            )

<script type="text/javascript">
    
    function onSuccess(e) {
        // Array with information about the uploaded files
        var files = e.files;
 
        if (e.operation == "upload") {
              document.getElementById("photopath").value = e.response.status;  
          }
        
        
        return false;
    }
 
</script>
The controler:
  public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> clientUpload)
        {
            string destinationPath = "";
            foreach (var file in clientUpload)
            {
                var filename = Path.GetFileName(file.FileName);
                destinationPath = Path.Combine(Server.MapPath("~/App_Data/client_Up"), filename);
                file.SaveAs(destinationPath);
                TempData["UploadedPath"] = destinationPath;
            }
            return Json(new { status = destinationPath }, "text/plain");
            
        }
 
        public ActionResult Remove(string[] fileNames)
        {
            foreach (var fullName in fileNames)
            {
                var fileName = Path.GetFileName(fullName);
                var deletePath = Path.Combine(Server.MapPath("~/App_Data/Client_Up"), fileName);
 
 
                if (System.IO.File.Exists(deletePath))
                {
                    System.IO.File.Delete(deletePath);
                }
                
            }
 
            return Content("");          
        
        }

This is on a create view, take it the same question goes for the edit part as well, note the above is still just testing and evaluating available options and yep, a bit new to mvc.

2 Answers, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 07 Jun 2011, 07:37 AM
Hello Jacob,

The Upload doesn't support binding to model properties. Your solution of using a (hidden) form field is very good.

You might not want to send the full file path to the client. A malicious user can modify it and gain access to other files. Instead, limit your uploads to a single folder and send only the short file name generated using Path.GetRandomFileName.

Greetings,
Tsvetomir Tsonev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jacob
Top achievements
Rank 1
answered on 07 Jun 2011, 10:50 AM
Cheers Tsvetomir

Appreciate the quick reply and the tip!

Regards
Jacob
Tags
Upload
Asked by
Jacob
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Jacob
Top achievements
Rank 1
Share this question
or