how to change the saveUrl in onUpload

6 posts, 0 answers
  1. Tom Ridenour
    Tom Ridenour avatar
    2 posts
    Member since:
    Aug 2009

    Posted 10 Jan 2012 Link to this post

    When a file is uploaded I need to change the saveUrl to add parameters for a post. This code has no effect on the saveUrl. 

    function onUpload(e) {
                        var upload = e.files;                   
                        
                        if (upload[0].extension != ".xml") {
                            alert("Only Xml files can be uploaded");
                            e.preventDefault();
                        }
                        else {
                            e.data = { fileName: upload[0].name };
                            kendoConsole.log("Upload :: " + getFileInfo(e));
                        }


                        //get the options and add to the Url
                        var option1 = $("#actions").val();
                        var postUrl = "PostViewData.svc/FileUpload/" + option1;
                        this.options.async.saveUrl = postUrl;
                    }
    How can the saveUrl be modified?

    $("#files").kendoUpload({
                            async: {
                            saveUrl: "PostViewData.svc/FileUpload",                            
                                autoUpload: false
                            },
                            multiple: false,
               showFileList: true,
                            select: onSelect,
                            upload: onUpload,
                            success: onSuccess,
                            error: onError,
                            complete: onComplete,
                            cancel: onCancel,
                            remove: onRemove,
                            localization : { uploadSelectedFiles: "Upload"}
                        });

    This is similar to adding a query string... saveUrl is already committed by the time the event onUploadFires?
  2. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 13 Jan 2012 Link to this post

    Hello,

    The saveUrl is indeed committed by the time the upload event fires. That said, you can add fields to the request by setting the data field of the event:
    function onUpload(e) {
        e.data = { actions: $("#actions").val() };
    }

    Is this solution applicable for your scenario?

    Regards,
    Tsvetomir Tsonev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Tom Ridenour
    Tom Ridenour avatar
    2 posts
    Member since:
    Aug 2009

    Posted 13 Jan 2012 Link to this post

    In this situation that will work because I have control of the REST service. But I think there maybe a way to modify the saveUrl when the user selects an option.

    For example:
    //dropdown

    $(

    "#actions").kendoDropDownList({

     

        change: onChange,

        dataSource: data,

        index: 0

    });

    //the onChange event

     

    function

     

    onChange() {

     

     

        var option1 = $("#actions").val();

     

    }

    I am new to jquery/javascript, I come from a Silverlight background. I think in the onChange event I can select the (#files)  kendoUploadControl and modify the saveUrl but I am not sure how to do that... any help is appreciated.

  5. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 16 Jan 2012 Link to this post

    Hi,

    Actually, the current implementation takes the saveUrl immediately after the file is selected. Changing it will have effect for newly selected files.

    There's no need to handle the dropdownlist change event, you can access its value in the upload handler, as illustrated earlier.

    I hope this helps.

    All the best,
    Tsvetomir Tsonev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Michael
    Michael avatar
    2 posts
    Member since:
    Jun 2013

    Posted 20 Feb 2014 in reply to T. Tsonev Link to this post

    Hi Tsvetomir,

    I'm trying to use this solution to pass a file count [not batch upload, the actual count of items contained by the control at the time an action is completed successfully], but i'm unsure how we need to process the "e.data" in the controller action.

    Can you provide a short code snippet that demonstrates how to access anything we put in e.data on the server side. We are using MVC / C#.

    Thanks in advance for any help you can offer!
    Michael
  7. Michael
    Michael avatar
    2 posts
    Member since:
    Jun 2013

    Posted 20 Feb 2014 Link to this post

    Hi all, we have coded a working solution that meets our needs. We did try to pass the necessary parameters in the "data" object, but while these were seen in the headers, they were not picked up on the controller action side of things. Here is what we're doing:

    var repl = "count=" + window.count;
    if (e.operation == "upload") {
        window.count++;
    }
    else if (e.operation == "remove") {
        window.count--;
    }                       
    $("#files").data("kendoUpload").options.async.saveUrl = $("#files").data("kendoUpload").options.async.saveUrl.replace(repl, "count=" + window.count);

    this is essentially the answer to the original post. YES, you can rewrite the saveUrl parameter. In our case, we're doing so in the onSuccess event when e.response.status == "OK", indicating that we've got a successful upload or delete.

    the above code is being used to do server side validation to prevent users from uploading more than the maximum number of files.

    cheers,
    Michael
Back to Top
Kendo UI is VS 2017 Ready