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

Capturing the filename from the upload component

1 Answer 41 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.
John
Top achievements
Rank 2
John asked on 15 Dec 2011, 09:01 PM

My application is a ASP.Net MVC 3 (razor) application and one of the tasks is to upload files and to log the uploads in a table .  To do this I want to use the upload component, but I have questions. 

To accomplish this I first created the controller and then all of the CRUD views of the log table.  I then modified the Create View and added the Telerik upload component to it.  The component works fine i.e. it copies the file from one directory to another. 

What I can’t figure out is that I need to capture the name of the file that was copied so I can save it in my log table.  The other question is how do I force the user to select a table before leaving the Create view?  Would the Telerik windows component be a good solution to this by configuring it to be a popup window?  Regardless I need to capture the file name.

If possible I’m hoping to avoid using any java script (I’m not very good with that).

Thanks John

1 Answer, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 19 Dec 2011, 10:07 AM
Hello John,

I assume you're using asynchronous upload.

The simplest option would be to log the uploaded file directly in the Save action. This would require no client-side code.

Capturing the file names on the client is also straight-forward. You can do so by handling the onSuccess client-side event:
function onSuccess(e) {
    if (e.operation == "upload") {
        // e.files contains list of uploaded files

   }
}

Sending them to the server is probably easiest to do using a hidden field.
function onSuccess(e) {
    if (e.operation == "upload") {
        for (var i = 0; i < e.files.length; i++) {
            var file = e.files[i];
            
    // Append one hidden input for each file.
            // myForm is the ID of the form
            $("<input type="hidden" name="files" />")
                .val(file.name)
                .appendTo($("#myForm"));
        }
    }
}

This will give you an array of strings in your server action. For example:
public ActionResult SaveForm(string[] files)

I hope this helps.

Kind regards,
Tsvetomir Tsonev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Upload
Asked by
John
Top achievements
Rank 2
Answers by
T. Tsonev
Telerik team
Share this question
or