Change the folder with parameter

Last post 03-19-2013, 4:06 PM by Kenneth. 3 replies.
Sort Posts: Previous Next
  •  03-15-2013, 11:51 AM 77047

    Change the folder with parameter

    Hello,

    I am trying to use your framework and I need to understand how is it possible to change the destination folder using the value of a field of my webpage. 

     

    In the method UploadHandler.ashx/OnFileUploaded, I achieved to save my images in an existing folder :

     

    file.CopyTo("~/Documents/" + file.FileName);

     

    but I want to do:

     

    file.CopyTo("~/Documents/" + valueOfMyField + "/" + file.FileName);

     

    I could overload the method with a string parameter which will represents my value or change the FileName but I don't know where I can change the call of the method (and if it's possible) or modify the FileName attribute.

     

    I've already taken a look to documentation or other posts in the forum, and I didn't find something close to my problem.

    Waiting for a reply, I thank you in advance for your attention.

     

     

    Filed under: , ,
  •  03-15-2013, 12:22 PM 77049 in reply to 77047

    Re: Change the folder with parameter

    Hi FlorentG,

     

    You can achieve the same thing in the controller directly, then you can get your text field value. Like the example below, you can save the upload file in the red part.

     

               public ActionResult selecting_multiple_files(string myuploader)
                {
                      using (CuteWebUI.MvcUploader uploader = new CuteWebUI.MvcUploader(System.Web.HttpContext.Current))
                      {
                            uploader.UploadUrl = Response.ApplyAppPathModifier("~/UploadHandler.ashx");
                            //the data of the uploader will render as <input type='hidden' name='myuploader'> 
                            uploader.Name = "myuploader";
                            uploader.AllowedFileExtensions = "*.jpg,*.gif,*.png,*.bmp,*.zip,*.rar";
                            //allow select multiple files
                            uploader.MultipleFilesUpload = true;
                            //tell uploader attach a button
                            uploader.InsertButtonID = "uploadbutton";
                            //prepair html code for the view
                            ViewData["uploaderhtml"] = uploader.Render();
                            //if it's HTTP POST:
                            if (!string.IsNullOrEmpty(myuploader))
                            {
                                  List<string> processedfiles = new List<string>();
                                  //for multiple files , the value is string : guid/guid/guid 
                                  foreach (string strguid in myuploader.Split('/'))
                                  {
                                        Guid fileguid = new Guid(strguid);
                                        CuteWebUI.MvcUploadFile file = uploader.GetUploadedFile(fileguid);
                                        if (file != null)
                                        {
                                              //you should validate it here:
                                              //now the file is in temporary directory, you need move it to target location
                                              //file.MoveTo("~/myfolder/" + file.FileName);
                                              processedfiles.Add(file.FileName);
                                        }
                                  }
                                  if (processedfiles.Count > 0)
                                  {
                                        ViewData["UploadedMessage"] = string.Join(",", processedfiles.ToArray()) + " have been processed.";
                                  }
                            }
                      }
                      return View();
                }
     

    Regards,

     

    Ken 

  •  03-19-2013, 3:43 PM 77065 in reply to 77049

    Re: Change the folder with parameter

    Hi,
    Thank you for your quick answer !
    I understand your solution but I tried to call a method of my controller (selecting_multiple_files) but it's never been hit by my breakpoint.
    The only thing (and the most important) that I didn't understand is how to call this method instead of OnFileUploaded (because when this method is called, it seems to be too late to get the value of my field).
     
    I tried to change the method called in the form:
    @{Html.BeginForm("selecting_multiple_files","trackerController");}
    @Html.Raw(ViewBag.uploaderhtml)
    @{Html.EndForm();}
     
    or is it possible to overwrite OnFileUploaded to put the field value with Javascript?

     

    Thank you,

    FlorentG
     

  •  03-19-2013, 4:06 PM 77066 in reply to 77065

    Re: Change the folder with parameter

    Hi FlorentG,

     

    it will fire when page post back, so you do not need to call it by code. the OnFileUploaded is fire after page post back too, so should be same as it.

     

    Regards,

     

    Ken 

View as RSS news feed in XML