views:

99

answers:

1

I'm trying to use the swfupload plugin to upload files. The thing is, I want the user to be able to switch the directory to upload to.

The strange thing is (and maybe this is due to my limited knowledge of jQuery) that it seems like I have the correct directory when checking it with an alert, but still the upload goes to the previously selected directory.

I.e. if I open the page, and click on the upload link, a div called uploadPanel is shown, which loads the swfupload. I can do the first upload fine. But if I then choose a different directory it doesn't work. I have put in an alert for testing that the variable for the current directory is correct. This alert always shows the current directory correctly. But still when the action method in the server code is called, the directory is always for the first selected directory that I had in the first upload.

Here's the jQuery code:

<script type="text/javascript">
    var uploadDir;
    $(function () {
        $("#uploadPanel").hide();
        var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
        loadFileManager(); 

        $("#uploadLink").click(function (event) {
            $('#uploadPanel').show();


            alert("uploadDir: " + uploadDir); //NOTE!!!: This shows the correct current directory, and yet, in post_params below, a previous directory is still sent to the action method in the Controller. How is this possible when the alert shows the correct directory?
            $("#inputFile").makeAsyncUploader({
                upload_url: "/Upload/AsyncUpload/",
                flash_url: '/Scripts/swfupload.swf',
                button_image_url: '/Scripts/blankButton.png',
                post_params: { token: auth, currentDirectory: uploadDir },
                use_query_string: true,
                disableDuringUpload: 'INPUT[type="submit"]'
            });

            $("#closeButton").click(function () {
                $("#uploadPanel").hide();
                $("#inputFile_completedMessage").hide();
            });
        });
    });        

</script>

Note: the uploadDir is set elsewhere, in a separate js file. But the point is, the value of it is always correct in the alert, so why not in the call to the action method?

A: 

You can change the value of POST params at will by calling the method: setPostParams() on the SWFUloader instance, so you don't have to re-create the instance every time.

ALSO, POST vars are just that arbitrary key/value pairs. In order for them to mean anything–in this case, to change the upload directory–you'll have to handle them in your server-side code, which I assume is here: /Upload/AsyncUpload (though I have doubts, as I would expect this to be something like /Upload/AsyncUpload.asp in this case).

Also, since you're using jQuery anyway, you should consider using the very handy jQuery.swfupload plugin. Makes like loads easier when working with SWFUploader.

http://github.com/ifunk/swfupload-jquery-plugin

mkoistinen
I don't know if I misunderstood you... I am using the swfupload plugin.
Anders Svensson