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

User selected directory for upload

2 Answers 112 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Matthew R.
Top achievements
Rank 1
Matthew R. asked on 03 Feb 2014, 06:01 PM
I'd like to provide a dropdown list of names to be used as part of the path to store files using the upload widget. Specifically, I'm using usernames as the directory name to store files. I have already figured out how to achieve this when the user is the one uploading files, however when an administrator does it then I need for them to be able to select the directory to upload the files to.

2 Answers, 1 is accepted

Sort by
0
Dimiter Madjarov
Telerik team
answered on 04 Feb 2014, 09:33 AM
Hello Matthew,


I am not sure that I completely understand the scenario with the users and the administrators. Nevertheless if you are referring to setting the saveUrl property of the Upload widget dynamically, you could achieve this in the upload event handler.
E.g.
function onUpload(e) {
    this.options.async.saveUrl = "newUrl";
}


Regards,
Dimiter Madjarov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Matthew R.
Top achievements
Rank 1
answered on 05 Feb 2014, 04:10 PM
Thanks for your prompt response. I ended up figuring it out. I've posted the code below for reference.
View:
@model IEnumerable<Project.Models.UploadInitialFile>
@using Kendo.Mvc.UI;
 
@Html.DropDownList("UserProfileId")
 
@(Html.Kendo().Upload()
        .Name("files")
        .Events(ev => ev
            .Upload("onUpload")
            .Remove("onRemove"))
        .Async(a => a
            .Save("SaveAndPersist", "Upload")
            .Remove("RemoveAndPersist", "Upload")
            .AutoUpload(true)
        )
        .Files(files =>
            {
                foreach (var f in Model)
                {
                    files.Add().Name(f.Name).Extension(f.Extension).Size(f.Size);
                }
            })
        )
 
<script>
    function onUpload(e) {
        e.data = { userProfileId: $('#UserProfileId').val() };
    }
 
    function onRemove(e) {
        e.data = { userProfileId: $('#UserProfileId').val() };
    }
</script>
 
// Controller:
 
public ActionResult Index()
        {
            // Get current user info
            ViewBag.UserProfileId = new SelectList(db.UserProfiles, "UserProfileId", "FullName");
 
            // Redisplay uploaded files to user after initial upload, gives them the option to delete them at that moment as well
            IList<UploadInitialFile> initialFiles =
                SessionUploadInitialFilesRepository.GetAllInitialFiles();
 
            return View(initialFiles);
        }
 
public async Task<ActionResult> SaveAndPersist(IEnumerable<HttpPostedFileBase> files, int? userProfileId)
        {
            // The Name of the Upload component is "files"
            if (files != null && userProfileId != null)
            {
                UserProfile up = await db.UserProfiles.FindAsync(userProfileId);
                var userDirectory = Path.Combine(Server.MapPath(ConfigurationManager.AppSettings["ClientUploadDirectory"]), up.UserName.ToString());
 
                // If current user doesn't have a directory in the uploads folder then create one
                if (!System.IO.Directory.Exists(userDirectory))
                {
                    System.IO.Directory.CreateDirectory(userDirectory);
                }
                foreach (var file in files)
                {
                    // Some browsers send file names with full path.
                    // We are only interested in the file name.
                    var fileName = Path.GetFileName(file.FileName);
                    var physicalPath = Path.Combine(userDirectory, fileName);
                    var fileExtension = Path.GetExtension(file.FileName);
 
                    SessionUploadInitialFilesRepository.Add(new UploadInitialFile(fileName, file.ContentLength, fileExtension));
 
                    // The files are saved in a userId named directory in the Uploads folder
                    file.SaveAs(physicalPath);
                }
            }
 
            // Return an empty string to signify success
            return Content("");
        }

@Html.DropDownList("UserProfileId")

 @(Html.Kendo().Upload()
        .Name("files")
        .Events(ev => ev
            .Upload("onUpload")
            .Remove("onRemove"))
        .Async(a => a
            .Save("SaveAndPersist", "Upload")
            .Remove("RemoveAndPersist", "Upload")
            .AutoUpload(true)
        )
        .Files(files =>
            {
                foreach (var f in Model)
                {
                    files.Add().Name(f.Name).Extension(f.Extension).Size(f.Size);
                }
            })
        )
Tags
Upload
Asked by
Matthew R.
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Matthew R.
Top achievements
Rank 1
Share this question
or