webForumDet fria alternativet

Redirect på nå vis

9 svar · 1 355 visningar · startad av Asa

AsaMedlem sedan dec. 20011 860 inlägg
#1

Har denna koden som anropas efter en upload av filer:

$(function(){

    var ul = $('#upload ul');

    $('#drop a').click(function(){
        // Simulate a click on the file input button
        // to show the file browser dialog
        $(this).parent().find('input').click();
    });

    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({

        // This element will accept file drag/drop uploading
        dropZone: $('#drop'),

        // This function is called when a file is added to the queue;
        // either via the browse button, or via drag/drop:
        add: function (e, data) {

            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
                ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            // Add the HTML to the UL element
            data.context = tpl.appendTo(ul);

            // Initialize the knob plugin
            tpl.find('input').knob();

            // Listen for clicks on the cancel icon
            tpl.find('span').click(function(){

                if(tpl.hasClass('working')){
                    jqXHR.abort();
                }

                tpl.fadeOut(function(){
                    tpl.remove();
                });

            });

            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit();
        },

        progress: function(e, data){

            // Calculate the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            // Update the hidden input field and trigger a change
            // so that the jQuery knob plugin knows to update the dial
            data.context.find('input').val(progress).change();

            if(progress == 100){
                data.context.removeClass('working');
            }
        },

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }

    });

    // Prevent the default action when a file is dropped on the window
    $(document).on('drop dragover', function (e) {
        e.preventDefault();
    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {
        if (typeof bytes !== 'number') {
            return '';
        }

        if (bytes >= 1000000000) {
            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {
            return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }

});

Nu vill jag att den ska skicka en vidare till en sida efter upload. Som det är nu är man kvar på uploadsidan. Man skall helst skickas vidare till en sida som heter photos.php?albumID=X där x på något vis skall komma med.

Skickar med variabeln album_id som hidden i formuläret där man väljer vilka filer man skall ladda upp.
Samt även hela länken som man ska skicka till efteråt. När man väl vet hur man gör. Ligger i variebeln backURL

Se exemplet här hur det är:
http://demo.tutorialzine.com/2013/05/mini-ajax-file-upload-form/

AsaMedlem sedan dec. 20011 860 inlägg
#2

Såg detta i en annan fil:

            // Set the following option to the location of a redirect url on the
            // origin server, for cross-domain iframe transport uploads:
            redirect: undefined,
            // The parameter name for the redirect url, sent as part of the form
            // data and set to 'redirect' if this option is empty:
            redirectParamName: undefined,

Länk till hela filen: https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload.js

Men vete tusan hur de menar..

Wiki info

redirect

Set this option to the location of a redirect url on the origin server (the server that hosts the file upload form), for cross-domain iframe transport uploads. If set, this value is sent as part of the form data to the upload server.
The upload server is supposed to redirect the browser to this url after the upload completes and append the upload information as URL-encoded JSON string to the redirect URL, e.g. by replacing the "%s" character sequence.

Type: string
Example: 'http://example.org/cors/result.html?%s'
redirectParamName

The parameter name for the redirect url, sent as part of the form data and set to 'redirect' if this option is empty.

Type: string
Example: 'redirect-url'

Snälla hjälp mig. SKa man skriva nåt på båda och vad menar de med ?%s ?

Har testat skriva länkar omfamnade av '' i de exemplen som är som de visar men inget händer efter upload.

AsaMedlem sedan dec. 20011 860 inlägg
#3

Ingen som vet ? jojoxx??

JojoxxMedlem sedan juni 20004 308 inlägg
#4

Asa skrev:

Ingen som vet ? jojoxx??

Har inte haft tid att läsa igenom wall-of-code, men du har ett block som ser ut så här:

            if(progress == 100){
                data.context.removeClass('working');
            }

Så ett hett tips är väl att göra det du vill göra när uppladdningen är klar där. (location.href=....)

Nate.AMedlem sedan mars 2006296 inlägg
#5
    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({
        /* Klippte bort din kod här för att det skulle bli lättare att se.. */
        done:function(e, data){
                /* Gör din redirect här */
        }
    });

I dokumentationen ser det ut som det finns en metod du kan använda dig av för att köra en funktion när uppladdningen är klar.
(rad 119 i exemplet: https://github.com/blueimp/jQuery-File-Upload/blob/master/basic.html#L119)

AsaMedlem sedan dec. 20011 860 inlägg
#6

Tackar allesammans :)

AsaMedlem sedan dec. 20011 860 inlägg
#7

Verkade inte fungera :/

voigtann1Medlem sedan juni 20019 519 inlägg
#8

Asa skrev:

Verkade inte fungera :/

hur ser din nuvarande kod som inte fungerade?

AsaMedlem sedan dec. 20011 860 inlägg
#9
$(function(){

    var ul = $('#upload ul');

    $('#drop a').click(function(){
        // Simulate a click on the file input button
        // to show the file browser dialog
        $(this).parent().find('input').click();
    });

    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({

        // This element will accept file drag/drop uploading
        dropZone: $('#drop'),

        // This function is called when a file is added to the queue;
        // either via the browse button, or via drag/drop:
        add: function (e, data) {

            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
                ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            // Add the HTML to the UL element
            data.context = tpl.appendTo(ul);

            // Initialize the knob plugin
            tpl.find('input').knob();

            // Listen for clicks on the cancel icon
            tpl.find('span').click(function(){

                if(tpl.hasClass('working')){
                    jqXHR.abort();
                }

                tpl.fadeOut(function(){
                    tpl.remove();
                });

            });

            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit();
        },

        progress: function(e, data){

            // Calculate the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            // Update the hidden input field and trigger a change
            // so that the jQuery knob plugin knows to update the dial
            data.context.find('input').val(progress).change();

            if(progress == 100){
                data.context.removeClass('working');
            }
        },

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }
        
        done:function(e, data){
            window.location.href='http://www.sidan.com/';
        }

    });

    // Prevent the default action when a file is dropped on the window
    $(document).on('drop dragover', function (e) {
        e.preventDefault();
    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {
        if (typeof bytes !== 'number') {
            return '';
        }

        if (bytes >= 1000000000) {
            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {
            return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }

});
    done:function(e, data){
        window.location.href='http://www.sidan.com/';
    }
JojoxxMedlem sedan juni 20004 308 inlägg
#10

Jag ser att det är ett syntax-fel på raden innan. Det saknas ett komma-tecken mellan fail och done-funktionerna:

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        },
        done:function(e, data){
            window.location.href='http://www.sidan.com/';
        }

Hett tips: Kolla loggen så ser du sådana syntax-fel :)

142 ms totalt · 3 externa anrop · v20260731065814-full.30151723
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
139 ms — hämta tråd, inlägg och bilagor (db)